Alloc, malloc and alloca - What's the difference?

I got the impression that alloc in Objective-C (when we call [anyObject alloc] , actually implements the C malloc function and the memory gets allocated on the heap, but cannot find the answer to this question.

Also, when searching for alloc I found alloca that allocates memory on the stack. If I am not mistaken, alloc allocates memory in a heap for creating objects.

So what is the difference between alloc and malloc (and alloca )? Can anyone summarize?

+7
c memory-management malloc memory alloca
source share
2 answers

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

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

alloca() allocates memory in the current frame of the function stack. Memory allocated using alloca() will be removed from the stack when the current function returns. alloca() limited to small selections.

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

+15
source share

The alloc function alloc used to allocate an area or block of bytes of size along the length of the heap .

The malloc function is used to allocate heap storage. His name means memory allocation.

0
source share

All Articles