C++ 中的“new”和“malloc”和“calloc”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/807939/
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 17:22:48  来源:igfitidea点击:

What is the difference between "new" and "malloc" and "calloc" in C++?

c++memory-managementmallocheap

提问by ?ukasz Lew

What is the difference between "new" and "malloc" and "calloc" and others in family?

"new" 和 "malloc" 和 "calloc" 与家族中的其他人有什么区别?

(When) Do I need anything other than "new" ?

(何时)我还需要“新”以外的任何东西吗?

Is one of them implemented using any other?

其中之一是使用其他实现的吗?

回答by Mehrdad Afshari

newand deleteare C++ specific features. They didn't exist in C. mallocis the old school C way to do things. Most of the time, you won't need to use it in C++.

new并且delete是 C++ 特定的功能。它们在 C 中不存在。malloc是老派的 C 做事方式。大多数情况下,您不需要在 C++ 中使用它。

  • mallocallocates uninitialized memory. The allocated memory has to be released with free.
  • callocis like mallocbut initializes the allocated memory with a constant (0). It needs to be freed with free.
  • newinitializes the allocated memory by calling the constructor (if it's an object). Memory allocated with newshould be released with delete(which in turn calls the destructor). It does not need you to manually specify the size you need and cast it to the appropriate type. Thus, it's more modern and less prone to errors.
  • malloc分配未初始化的内存。分配的内存必须用 释放free
  • calloc就像malloc但用常量(0)初始化分配的内存。它需要通过free.
  • new通过调用构造函数来初始化分配的内存(如果它是一个对象)。分配的内存new应该被释放delete(它反过来调用析构函数)。它不需要您手动指定所需的大小并将其转换为适当的类型。因此,它更现代,更不容易出错。

回答by Brian R. Bondy

new/delete + new[]/delete[]:

新建/删除+新建[]/删除[]:

  • new/delete is the C++ way to allocate memory and deallocate memory from the heap.
  • new[] and delete[] is the c++ way to allocate arrays of contiguous memory.
  • Should be used because it is more type safe than malloc
  • Should be used because it calls the constructor/destructor
  • Cannot be used in a realloc way, but can use placement new to re-use the same buffer of data
  • Data cannot be allocated with new and freed with free, nor delete[]
  • new/delete 是从堆分配内存和释放内存的 C++ 方式。
  • new[] 和 delete[] 是分配连续内存数组的 C++ 方式。
  • 应该使用,因为它比 malloc 类型更安全
  • 应该使用,因为它调用构造函数/析构函数
  • 不能以realloc方式使用,但可以使用placement new来重用相同的数据缓冲区
  • 数据不能用new分配,用free释放,也不能用delete[]

malloc/free + family:

malloc/free + 系列:

  • malloc/free/family is the C way to allocate and free memory from the heap.
  • calloc is the same as malloc but also initializes the memory
  • Should be used if you may need to reallocate the memory
  • Data cannot be allocated with malloc and freed with delete nor delete[]
  • malloc/free/family 是从堆分配和释放内存的 C 方法。
  • calloc 与 malloc 相同,但也初始化内存
  • 如果您可能需要重新分配内存,应该使用
  • 数据不能用 malloc 分配,不能用 delete 或 delete[] 释放

Also see my related answer here

另请参阅我的相关答案here

回答by dirkgently

  • new allocates and calls to ctor (the order is unspecified), delete the dtor and frees the memory allocated by a call to new
  • malloc only allocates some memory, free deletes memory allocated by malloc
  • new may be implemented using malloc (not required though by the standard)
  • calloc does the same thing as malloc and also zeroizes the newly allocated memory
  • new 分配并调用 ctor(顺序未指定),删除 dtor 并释放调用 new 分配的内存
  • malloc只分配部分内存,free删除malloc分配的内存
  • new 可以使用 malloc 实现(尽管标准不需要)
  • calloc 与 malloc 做同样的事情,并将新分配的内存归零

As other posts have pointed out: malloc/free is part of C++ to be compatible with C.

正如其他帖子指出的那样:malloc/free 是 C++ 的一部分,与 C 兼容。

Also see: Stroustrup: new vs malloc

另请参阅:Stroustrup:new vs malloc

回答by dirkgently

Using new means that constructors will be called on the newly allocated memory. If the thing being allocated doesn't have constructors, new is functionally identical to malloc. and should normally be used in pereference to it.

使用 new 意味着将在新分配的内存上调用构造函数。如果被分配的事物没有构造函数,则 new 在功能上与 malloc 相同。并且通常应该用于优先考虑它。

new may or may not be implemented in terms of malloc - the C++ standard does not require either approach.

new 可能会或可能不会根据 malloc 实现 - C++ 标准不需要任何一种方法。

回答by Steve Rowe

You don't need anything other than new. It is a complete replacement for malloc in C++.

除了新的,你不需要任何东西。它是 C++ 中 malloc 的完全替代品。

As for the difference: Malloc just allocates memory. New allocated memory and calls the constructors. Likewise free just releases the memory. Delete releases the memory and calls the destructor.

至于区别:Malloc 只是分配内存。新分配的内存并调用构造函数。同样 free 只是释放内存。Delete 释放内存并调用析构函数。

A word of warning: Don't mix the two idioms. The results are undefined.

一句警告:不要混用这两个习语。结果是不确定的。

回答by z -

the main difference between new and malloc I can recall is that you cannot reallocate memory allocated by new using realloc. So if you wanted to increase/decrease the size of the memory block, you had to allocate a new block and copy everything over.

我记得 new 和 malloc 之间的主要区别是您不能使用 realloc 重新分配由 new 分配的内存。因此,如果您想增加/减少内存块的大小,则必须分配一个新块并复制所有内容。

Calloc allows you to initialize the memory block you allocate while malloc does not.

Calloc 允许您初始化您分配的内存块,而 malloc 则不允许。

回答by TStamper

When you newan object, space for the object is not only allocated but the object's constructor is called. But this is the C++ way its done, mallocis the old version way in Cof allocating memory. callocis the same as malloc, except for it clears memory to all bits zero.

当您新建一个对象时,不仅会为该对象分配空间,还会调用该对象的构造函数。但这是 C++ 的方式,mallocC中分配内存的旧版本方式。callocmalloc相同,除了它将内存清除为所有位为零。