C语言 alloc、malloc 和 alloca — 有什么区别?

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

alloc, malloc, and alloca — What's the difference?

cmemorymemory-managementmallocalloca

提问by Anindya Sengupta

I was under the impression that allocin Objective-C (when we invoke [anyObject alloc]is actually implementing C function mallocand the memory getting allocated in heap, but could not find anywhere the answer for this.

我的印象是alloc在 Objective-C 中(当我们调用[anyObject alloc]实际上是在实现 C 函数malloc并且内存在堆中分配,但在任何地方都找不到答案。

Also, while searching for alloc, I found allocawhich allocates memory in stack. If I am not wrong, allocallocates memory in heap to create objects.

另外,在搜索 时alloc,我发现alloca它在堆栈中分配内存。如果我没记错的话,alloc在堆中分配内存来创建对象。

So, what is the difference between allocand malloc(and alloca)? Can anyone please summarize?

那么,allocmalloc(和alloca)之间有什么区别?有人可以总结一下吗?

回答by duskwuff -inactive-

alloc()is not a standard C library function. Some older compilers and libraries contain an <alloc.h>library which provides some memory allocation functions, but this is not standard. The Microsoft Visual C++ runtime includes an Alloc()function which is somewhat similar to malloc(), but this is also not part of the C standard.

alloc()不是标准的 C 库函数。一些较旧的编译器和库包含<alloc.h>提供一些内存分配功能的库,但这不是标准的。Microsoft Visual C++ 运行时包含一个Alloc()类似于的函数malloc(),但这也不属于 C 标准的一部分。

malloc()allocates memory on the process heap. Memory allocated using malloc()will remain on the heap until it is freed using free().

malloc()在进程堆上分配内存。使用分配的内存malloc()将保留在堆上,直到使用 释放free()

alloca()allocates memory within the current function's stack frame. Memory allocated using alloca()will be removed from the stack when the current function returns. alloca()is limited to small allocations.

alloca()在当前函数的堆栈帧内分配内存。alloca()当当前函数返回时,使用分配的内存将从堆栈中删除。alloca()仅限于小分配。

Situations where alloca()is appropriate are rare. In almost all situations, you should use malloc()to allocate memory.

情况下alloca()是合适的并不多见。在几乎所有情况下,您都应该使用malloc()来分配内存。

回答by Girish Singh rathore

The allocfunction is used to allocate a region or block of size bytes in length of the heap.

alloc函数用于分配大小字节大小的区域或块heap

The mallocfunction is used to allocate heapstorage. Its name stands for memory allocation.

malloc函数用于分配heap存储。它的名字代表内存分配。

回答by Girish Singh rathore

I don't remember the verbatim statement from the book C++ Primer, but there is a major difference between the functions. For example new in C++ allocates memory, but it also constructs the data into the memory. The std::allocator allocates memory, but doesn't call any constructor. The same is true for these C functions. One allocates but doesn't construct. One allocates and construct.

我不记得 C++ Primer 一书中的逐字陈述,但函数之间存在重大差异。例如 C++ 中的 new 分配内存,但它也将数据构造到内存中。std::allocator 分配内存,但不调用任何构造函数。这些 C 函数也是如此。一个分配但不构造。一是分配构造。