略微混乱的指针情况 [英] Slightly confusing pointer situation

查看:82
本文介绍了略微混乱的指针情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我不完全确定指针在我创建的这种情况下是如何工作的,我小心避免内存泄漏。我写了以下代码;



Hi,

I'm not entirely sure how pointers work in this situation I've created and I'm being careful to avoid memory leaks. I've written the following code;

Packet* packet;

// construct packet from ID and data
switch (packet_id)
{
case 0:
	PacketPlayerMove* p = new PacketPlayerMove();
	// call some methods on p here which aren't present in base class Packet
	packet = p;
}

return packet;



所以PacketPlayerMove和其他许多人一样是基类Packet的实现。此方法返回Packet *,因此我创建Packet *并将其值指定为任何继承类的实例,具体取决于唯一标识符packet_id。



简而言之:



packet_id 0:返回PacketPlayerMove

packet_id 1:返回PacketPlayerChat





我不确定是否应该删除 p ,因为我不确定是否会这样做删除数据包,因为它的引用或值被复制 - 这是我的指针知识朦胧的地方。



我稍后会删除数据包,但我不知道我现在是否应该删除p,或者是否需要删除它。



我的尝试:



----------- ------------------------------------


So PacketPlayerMove and many others are implementations of the base class Packet. This method returns a Packet*, so I create a Packet* and assign its value as an instance of any of the inheriting classes, depending on a unique identifier packet_id.

In a nutshell:

packet_id 0: return PacketPlayerMove
packet_id 1: return PacketPlayerChat
etc.

I'm not sure whether I should delete p or not, because I'm not sure if that'll delete packet, as either its reference or value is copied - this is where my knowledge of pointers is hazy.

I will be deleting packet later, but I don't know whether I should delete p now, or if it doesn't need to be deleted.

What I have tried:

-----------------------------------------------

推荐答案

你也可以查看 C ++ 智能指针,例如参见intro/smart pointers - cppreference.com [ ^ ]。
You may also have a look at C++ smart pointers, see for instance intro/smart pointers - cppreference.com[^].


如果您稍后要删除指针,则不应该立即删除它。如果你复制了它,那么你可能会有内存泄漏。您应该能够测试它并检测内存泄漏。我在整个地方使用#define new DEBUG_NEW宏,并在调试模式下检测到泄漏。
If you are going to delete the pointer later then you should not delete it 'now'. If you have copied it then you will likely have a memory leak. You should be able to test this and detect a memory leak. I use the "#define new DEBUG_NEW" macro all over the place and leaks are detected in debug mode.


你必须明白指针变量就像一个普通变量。因此,您可以为它们分配不同的值,或为不同的指针分配相同的值。但是创建一个带有新的实例需要删除其他地方。

You must understand that a pointer variable is like a normal variable. So you can assign different values to them or the same value to different pointers. But creating an instance with new needs a delete somewhere else.
//some normal int var
int a = 2;
int b = a;

PacketPlayerMove* p = new PacketPlayerMove();//Pointer value NEEDS a delete
PacketPlayerMove* p2 = p;//no new instance only pointer assigment, so both pointers are pointing to the SAME address

delete p;// free the memory.

了解指针 C++教程。它有一些有趣的章节,里面有很多有趣的知识。



最好的做法是编写清晰易懂的代码,因为在处理大量指针时,很多东西都可以出错了。

Learn about pointers in this C++ tutorial. It has some interesting chapters packed with interesting knowledege.

It is best practice to write clear and understandable code, because when working with a lot of pointers a lot of things can go wrong.


这篇关于略微混乱的指针情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆