你能在 C++ 中混合使用 free 和构造函数吗?

发布于 2024-09-18 19:33:00 字数 836 浏览 9 评论 0原文

可能的重复:
调用 free 有什么危险吗( ) 或删除而不是删除[]?

我正在阅读这个问题:
在什么情况下使用 malloc 与 new?

有人提出使用 malloc 的原因之一是如果您要使用 free。

我想知道:在 C++ 中混合自由调用和构造函数初始化是否有效?

我可以说:

my_type *ptr = new my_type;
free(my_type);

这是否在某种程度上无效或更糟糕

my_type *ptr = new my_type;
delete my_type;

除了它不是 c++ 的事实之外,

?同样,你能做相反的事吗?你能说,

my_type *ptr = (my_type *)malloc(sizeof(my_type));
delete my_type;

如果这是重复的,请合并,我搜索过,但没有看到关于 malloc/delete/new/free 的问题。

Possible Duplicate:
Is there any danger in calling free() or delete instead of delete[]?

I was reading this question:
In what cases do I use malloc vs new?

Someone raised that one reason to use malloc was if you were going to use free.

I was wondering: Is it valid to mix a free call and a constructor initialization in C++?

i.e.

Can I say:

my_type *ptr = new my_type;
free(my_type);

Is that somehow invalid or worse than:

my_type *ptr = new my_type;
delete my_type;

other than the fact that it's not c++ish?

Likewise, could you do the opposite? Can you say

my_type *ptr = (my_type *)malloc(sizeof(my_type));
delete my_type;

Please merge if this is a duplicate, I searched but didn't see a question along this lines exactly about malloc/delete/new/free asked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神爱温柔 2024-09-25 19:33:00

不,它是无效的。不保证 new 将使用 mallocdelete 将使用 free

此外,使用free而不是delete将跳过my_type的析构函数。如果 my_type 本身持有一些资源,这些资源将会被泄漏。同样,malloc 将跳过构造函数,因此变量可能处于无效状态。

No it is invalid. There is no guarantee that new will use malloc or delete will use free.

Moreover, using free instead of delete will skip my_type's destructor. If my_type itself is holding some resources, those will be leaked. Similarly, malloc will skip the constructor so the variable may be in an invalid state.

雨夜星沙 2024-09-25 19:33:00

在 C++ 中混合自由调用和构造函数初始化是否有效?

不,不是。

malloc、calloc、realloc ->免费

新->删除

新的[]->删除[]

Is it valid to mix a free call and a constructor initialization in C++?

No it is not.

malloc,calloc,realloc -> free

new -> delete

new[] -> delete[]

画离情绘悲伤 2024-09-25 19:33:00

不,这是无效的。 malloc(和其他 C 分配函数)必须与 free 匹配,new 必须与 delete 匹配,并且new [] 必须与delete [] 匹配。虽然如果没有析构函数代码,您的编译器不一定会执行任何不同的操作,但这不是您应该依赖的东西。

主要区别在于new/delete调用对象的构造函数和析构函数; malloc 和 free 只是将其视为原始的、无类型的内存。

No, this is not valid. malloc (and other C allocation functions) must be matched with free, new must be matched with delete, and new [] must be matched with delete []. While your compiler may not necessarily do anything differently if there is no destructor code, this is not something you should rely on.

The main difference is that new/delete call the constructor and destructor of an object; malloc and free just treat it as raw, untyped memory.

把时间冻结 2024-09-25 19:33:00
  1. free() 不调用析构函数。它只是释放内存,不问任何问题。

  2. new/delete 不保证使用 malloc() 作为其内存分配器; free 甚至可能不知道你扔给它的内存

  1. free() does not call the destructor. It just deallocates the memory, no questions asked.

  2. new/delete is not guaranteed to use malloc() as its memory allocator; free might not even know about the memory you're throwing at it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文