删除指针是什么意思? [英] What does deleting a pointer mean?

查看:213
本文介绍了删除指针是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

删除指针它指向,或者)意味着

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

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

p = new type;

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

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

free(p);

以前使用malloc或calloc分配

which was previously allocated using malloc or calloc

p = malloc(size);

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

The latter is more often referred to as "freeing", while the former is more often called "deleting". delete is used for classes with a destructor since delete will 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.

这篇关于删除指针是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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