In C ++, passing a pointer still copies the object?

I've been reading for an hour and still don't understand what's going on with my application. Since I am using object instances with new

and delete

, I need to manage the memory myself. My application needs to have long periods of time, so proper memory management is very important to me.

Here's a static function that I use to dump a data packet that is transferred between the PC and the I / O board in both directions. A data packet is an array BYTE

and is encapsulated in an object DCCmd

, either DCReply

(both are implementations of the abstract class DCMessage

).

void DebugTools::dumpBytes(BYTE* bytes, short length)
{
    printf("       |---DUMPING DATAPACKET refId: %d ....\n", &bytes);
    for(short i=0; i<length; i++){
        printf("       | B%d | %.2X\n", i, bytes[i]);
    }
    printf("       |---END DUMP           refId: %d ....\n", &bytes);
}

      

Then there is this use case: I create an object DCCmd

and add it to the queue of outgoing messages to be sent. The "pump" (infinite loop) checks the source code and passes any candidates for the IOConnector

singleton object .

DCCmd* cmd = new DCCmd(DIG_CMD_SELFTEST_RES);
cmd->add(param);
printf("cmdSelfTest()\n"); //HACK
BYTE* cmda = cmd->getBytes(); //HACK
DebugTools::dumpBytes(cmda, cmd->getLength()); //HACK
sendMsg(cmd);

      

... and adding to the queue:

bool DC::sendMsg(DCMessage* msg)
{
    if(isOnline()){
        outbox->add(msg);
        return true;
    } else {
        return false;
    }
}

      

Adding to the queue is done with void add(DCMessage* msg);

(There is another one in the connector class dumpBytes()

to see what will actually be sent)

But here's the output:

TESTING MESSAGE QUEUES ....
cmdSelfTest()
       |---DUMPING DATAPACKET refId: 2489136 ....
       | B0 | C6
       | B1 | A1
       | B2 | 00
       | B3 | 01
       | B4 | 10
       | B5 | 00
       | B6 | 01
       | B7 | 78
       |---END DUMP           refId: 2489136 ....
    adding to queue: 2488884
   queues: inbox (0), outbox (1)
send: sending candidates....
  sending 2489164 ....
    >->-> ...
       |---DUMPING DATAPACKET refId: 2488704 ....
       | B0 | C6
       | B1 | A1
       | B2 | 00
       | B3 | 01
       | B4 | 10
       | B5 | 00
       | B6 | 01
       | B7 | 78
       |---END DUMP           refId: 2488704 ....
Packet sent!
. ((second iteration of the pump))
   queues: inbox (0), outbox (1)
send: sending candidates....
  sending 2489164 ....
    >->-> ...
       |---DUMPING DATAPACKET refId: 2488704 ....
       | B0 | C6
       | B1 | A1
       | B2 | 00
       | B3 | 01
       | B4 | 10
       | B5 | 00
       | B6 | 01
       | B7 | 78
       |---END DUMP           refId: 2488704 ....
Packet sent!

      

Can someone shed some light on why the links are different every time I move from one block to another? What does this mean for memory consumption? How can I make sure I am not duplicating memory? Thank.

+2


source to share


3 answers


Variable bytes are a pointer to data, that is, the location of the data memory. But that is not what you are printing, you are printing the address where this pointer is located, that is, the address on the stack where the pointer is passed. So

printf("       |---DUMPING DATAPACKET refId: %d ....\n", &bytes);

      



it should be

printf("       |---DUMPING DATAPACKET refId: %d ....\n", bytes);

      

+5


source


The variable bytes is a function parameter in dumpBytes, in this case a pointer, you get a new pointer copied from the passed pointer, but it is still new, with its own address on the stack, so it will be different with its address every time, unless it is not called from the same place, and the stack addresses lead to the same by pure coincidence.



+1


source


In your dumpBytes calls, you passed bytes using pass-by-copy instead of pass-by-reference.

This results in a new pointer for BYTES created for dumpBytes lifetime. Depending on your system, this will be 8,16,32,64 bytes, etc. In other words, if you don't have really hard memory constraints, then this is not a problem.

+1


source







All Articles