40

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

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

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

Anindya Sengupta
  • 2,539
  • 2
  • 21
  • 27

3 Answers3

70

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.

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.

  • 18
    "Situations where alloca() is appropriate are rare. In almost all situations, you should use malloc() to allocate memory." No. All `alloca()` does is provide variable length arrays. There's no practical difference between `int n = 6; double x[n];` and `int n = 6; double *x = alloca( n * sizeof( *x ) );` Not only that, the use of `malloc()` in multithreaded programs can be a severe performance bottleneck. – Andrew Henle Sep 21 '15 at 01:24
  • My query was more in terms of Objective-C which implements `[[someObject alloc] init]` to allocate the memory and initialize the object in heap. Does this `[someObject alloc]` implements `malloc` in the background? – Anindya Sengupta Sep 21 '15 at 01:25
  • 18
    @AndrewHenle I didn't say that there are *no* situations where `alloca()` is appropriate; I said that they're rare. VLAs are one case where they're useful, but even there they're essentially an optimization. Since the OP is clearly new to C programming, I opted to not go to deep into the details. :) –  Sep 21 '15 at 01:43
  • 3
    @AnindyaSengupta Your question didn't mention Objective-C, so I didn't consider that as a possibility. Yes, the `+[NSObject alloc]` method will most likely call `malloc()`, or something functionally equivalent. –  Sep 21 '15 at 01:44
  • 3
    @Andrew Henle, there is **real** practical difference between `alloca` and VLA's if you use in a loop. – ideasman42 Sep 21 '15 at 02:01
  • So `alloca()` is garbage-collected `malloc()`? Sweet! – étale-cohomology Oct 08 '18 at 22:14
  • 3
    @étale-cohomology I’d hesitate to call it “garbage collected”. Frame-local, sure. But nothing fancier than that. –  Oct 09 '18 at 02:19
  • 5
    failed to mention that `alloca()` is apparently BSD\GNU and isn't in standard of C or POSIX – Swift - Friday Pie Oct 19 '19 at 22:09
  • 1
    also memory still need to be freed, it's not freed automatically if pointer goes out of scope, it's freed only when function returns. – Swift - Friday Pie Oct 19 '19 at 22:14
-5

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

The malloc function is used to allocate heap storage. Its name stands for memory allocation.

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
-9

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.