简体   繁体   English

在C ++中,传递指针仍会复制对象吗?

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

I've been reading for an hour now and still don't get what is going on with my application. 我已经阅读了一个小时,但仍然不了解我的应用程序正在发生什么。 Since I am using instances of object with new and delete , I need to manage the memory myself. 由于我使用带有newdelete的对象实例,因此我需要自己管理内存。 My application needs to have long uptimes and therefore properly managing the memory consumption is very crucial for me. 我的应用程序需要长时间运行,因此正确管理内存消耗对我来说至关重要。

Here's the static function I use to dump the datapacket, which is transfered between a PC and the I/O board in both directions. 这是我用来转储数据包的静态函数,该数据包在PC和I / O板之间双向传输。 The datapacket is an array of BYTE s and is encapsulated into an object, either DCCmd or DCReply (both are implementation of an abstract DCMessage class). 数据包是BYTE的数组,并封装到一个对象中,该对象可以是DCCmdDCReply (两者都是抽象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's this use case: I create a DCCmd object and add it to the outgoing message queue to be sent. 然后是这种用例:我创建一个DCCmd对象,并将其添加到要发送的外发消息队列中。 The "pump" (an infinite loop) checks the outbox and passes any candidates to a IOConnector singleton object. “泵”(无限循环)检查发件箱,并将所有候选项传递给IOConnector单例对象。

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); 添加到队列是通过void add(DCMessage* msg);

(In the connector class there's another of those dumpBytes() to see what is really going to be sent) (在连接器类中,还有另一个dumpBytes()用来查看实际发送的内容)

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 please shed some light why the references are different each time I pass from one block to the other? 有人可以阐明我每次从一个块传递到另一个块时引用为何不同的原因吗? What does this mean to the memory consumption? 这对内存消耗意味着什么? How can I make sure I am not duplicating memory? 如何确定我没有复制内存? Thanks. 谢谢。

The variable bytes is the pointer to the data, ie the memory location of the data. 可变字节是指向数据的指针,即数据的存储位置。 But that is not what you are printing, you are printing out the address where this pointer is located, ie the address on the stack where the pointer is passed. 但这不是您要打印的内容,而是要打印出该指针所在的地址,即指针所传递的堆栈上的地址。 So 所以

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

should just be 应该只是

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

bytes变量是dumpBytes中的一个函数参数,在本例中为指针,您会从传递的指针中复制一个新指针,但它仍然是一个新指针,在堆栈中有自己的地址 ,因此取其地址将每次都不同,除非它是从同一位置调用的,并且堆栈地址由于纯巧合而导致相同。

In your calls to dumpBytes you passed the bytes using pass-by-copy instead of pass-by-reference. 在对dumpBytes的调用中,您使用了传递副本而不是传递引用来传递字节。

This results in a new pointer to BYTES being created for the lifetime of the dumpBytes. 这将导致在dumpBytes的生存期内创建指向 BYTES的新指针 Depending on your system this will be 8,16,32,64 bytes etc. In otherwords, unless you've got really, really tight memory constraints then it's not an issue. 根据您的系统,这将是8,16,32,64字节等。换句话说,除非您有非常严格的内存限制,否则这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM