0

Possible Duplicate:
Difference between declaration and malloc

what's the difference between:

char * a = malloc(size);  

and

char a[size];  

Is one better than the other? is there any advantage at all in using malloc?

Community
  • 1
  • 1

2 Answers2

3

You don't control the life-time scope of stack allocated memory, it's only valid for as long as the scope (unless you make it static).

malloc is for allocating memory on the heap. It's valid until you call free on that memory.

It's faster to allocate memory on the stack, normally because you're not actually allocating new memory you're just reserving more of what you're already using but you don't control the life-time, it controlled by the scope of your block or function.

John Leidegren
  • 59,920
  • 20
  • 131
  • 152
  • wow thank you so much for the fast answer! can you give me an example where malloc works better? If it's not asking too much, because I'd like to know in which cases the declaration would fail – user1907948 Dec 16 '12 at 14:32
  • Stack allocations fails when there's no stack space left, infinite recursion results in the same problem, stack overflow. Heap allocation gives you access to the heap, it's probably a lot larger than the stack space you have reserved but not infinite or without it's problems. Use the heap when you intend to share things for longer periods of time but be mindful of things like heap fragmentation. – John Leidegren Dec 16 '12 at 14:37
2

what's the difference between char *a = malloc(size); and char *a[size];`?

The first one declares a pointer to char and allocates size bytes for it on the heap. The second one allocates size pieces of char pointers. They're not equivalent.

Is the one better than the other?

No, they have different uses.

Is there any advantage at all in using malloc()?

Yes. If you want to return an array from a function, you can't do this:

char a[size];
return a;

because then a is out of scope after the return, and using it results in undefined behavior. So in this case you must use

char *a = malloc(size);
return a;

(Generally this is the case, since you would presumably want to return a new string each time from a function - however, if this is not a requirement, you can use a static array, declared locally.)

However, if the array is only to be used locally, then it's generally advisable to use auto (stack) arrays and not malloc() because that avoids increasing memory fragmentation and stack operations can be faster than heap access.

  • 1
    "If you want to return an array from a function, ..., you must use ... " isn't entirely correct -- `static char a[size]; return a;` should also work. – Joshua Green Dec 16 '12 at 14:34
  • @JoshuaGreen I didn't write "you can't do `static char a[size];`", I wrote "you can't do `char a[size];`"... –  Dec 16 '12 at 14:36
  • @JoshuaGreen In other words, declaring an array with the `static` keyword does not result in an auto array, since its storage duration is static... –  Dec 16 '12 at 14:37
  • I was disagreeing with the claim that one "must use `char *a = malloc(size); return a;`" by providing an alternative. – Joshua Green Dec 16 '12 at 14:40
  • @JoshuaGreen Now it's precise, updated. –  Dec 16 '12 at 14:41
  • I would also mention that usually there is a bigger chance to have cache hits in the stack. – Ramy Al Zuhouri Dec 16 '12 at 14:42