C++ 删除指针是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2449386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:31:46  来源:igfitidea点击:

What does deleting a pointer mean?

c++pointersmemory-management

提问by Ashish Yadav

Is deleting a pointer same as freeing a pointer (that allocates the memory)?

删除指针是否与释放指针(分配内存)相同?

回答by Johannes Schaub - litb

Deleting a pointer (or deleting what it points to, alternatively) means

删除一个指针(或者删除它指向的内容)意味着

delete p;
delete[] p; // for arrays

pwas allocated prior to that statement like

p在该语句之前分配,例如

p = new type;

It may also refer to using other ways of dynamic memory management, like free

它也可能指使用其他动态内存管理方式,例如 free

free(p);

which was previously allocated using malloc or calloc

之前使用 malloc 或 calloc 分配的

p = malloc(size);

The latter is more often referred to as "freeing", while the former is more often called "deleting". deleteis used for classes with a destructor since deletewill call the destructor in addition to freeing the memory. free(and malloc, calloc etc) is used for basic types, but in C++ new and delete can be used for them likewise, so there isn't much reason to use malloc in C++, except for compatibility reasons.

后者更常被称为“释放”,而前者更常被称为“删除”。delete用于具有析构函数的类,因为delete除了释放内存之外还会调用析构函数。free(和 malloc、calloc 等)用于基本类型,但在 C++ 中 new 和 delete 也可以用于它们,因此除了兼容性原因外,没有太多理由在 C++ 中使用 malloc。

回答by fredoverflow

You can't "delete" a pointer variable

你不能“删除”一个指针变量

Sure you can ;-)

你当然可以 ;-)

int** p = new int*(new int(42));
delete *p;
delete p; // <--- deletes a pointer

But seriously, deleteshould really be called delete_what_the_following_pointer_points_to.

不过说delete真的,真的应该叫delete_what_the_following_pointer_points_to

回答by Hyman

Yes, deleteis used to deallocate memory and call the destructor for the object involved.

是的,delete用于释放内存并为所涉及的对象调用析构函数。

It's common pratice to set pointer to NULLafter deleting it to avoid having invalid pointers around:

NULL删除指针后设置指针是一种常见的做法,以避免周围出现无效指针:

Object *o = new Object();

// use object
delete o; // call o->~Object(), then releases memory
o = NULL;

When newand deleteare used with standard C types in C++ source they behave like mallocand free.

newdelete与 C++ 源代码中的标准 C 类型一起使用时,它们的行为类似于mallocfree

回答by Hyman

Yes deleting a pointer is the same as deallocating memory or freeing memory, etc.

是的,删除指针与释放内存或释放内存等相同。

回答by Alen Sindicic

You can't "delete" a pointer variable, only set their value to NULL (or 0).

您不能“删除”指针变量,只能将它们的值设置为 NULL(或 0)。

回答by Chris Thompson

Yes and it calls the appropriate destructor.

是的,它调用适当的析构函数。

回答by shinjin

In short, yes.

简而言之,是的。

But you have to be careful: if you allocate with p = new sometype()only then should you use delete p. If you allocate using p = sometype[count]always use delete [] p

但是你必须小心:如果你p = new sometype()只分配,那么你应该使用delete p. 如果您分配使用p = sometype[count]始终使用delete [] p

And one more thing: you should never pair malloc/deleteor new/free.

还有一件事:你永远不应该配对malloc/deletenew/free