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

前端 未结 3 819
逝去的感伤
逝去的感伤 2021-01-31 04:07

I was under the impression that alloc in Objective-C (when we invoke [anyObject alloc] is actually implementing C function malloc and the

3条回答
  •  无人及你
    2021-01-31 04:36

    alloc() is not a standard C library function. Some older compilers and libraries contain an 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.

    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 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.

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

提交回复
热议问题