2

It is often said to use malloc when size is known at run time we could also write

int x;
scanf("%d",&x);
char arr[x];

so why use malloc when we can declare array on the fly.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 2
    @YassineHoussni ugh, huuge assertions. The "it's faster" statement is even wrong. Dynamic memory allocation is one of the slowest "primitive" operations. Usually, declaring a VLA makes memory available almost instantly (it's only the stack pointer that needs to be added/subtracted a certain number), unlike `malloc()` which involves searching through a free store (potentially and often implemented using complex data structures with large constant factors). – The Paramagnetic Croissant Feb 16 '15 at 10:47
  • Someone should also remember that while, a `malloc()` requires `free()` VLA's don't, and that is very important specially for large arrays. – Iharob Al Asimi Feb 16 '15 at 10:50

2 Answers2

5

Writing char arr[x]; will allocate the memory on the stack.

The size of the stack is typically limited to around 1MB. You'll get runtime errors if you exceed this pre-defined amount. Some compilers will allow you to change the stack size, but you'll still hit a limit eventually of many orders of magnitude than you can get with malloc.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • so the size of memory needed and the scope of the data decides when to use static or dynamic.Is there any other reason? – Bijeet chatterjee Feb 16 '15 at 10:45
  • 2
    I don't think the stack is typically so small, and all of the answers lack any advice on using `free()` which in the case of using `malloc()` just because the stack doesn't fit the data for it's size, is really really important. – Iharob Al Asimi Feb 16 '15 at 10:48
  • @iharob: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686774%28v=vs.85%29.aspx 1 MB by default on Windows (requested size stored in the executable), 8 MB by default on my Linux 64 bit machine (as per `ulimit -s`). I wouldn't try to load an image or anything biggish on it (especially since stack overflows are particularly nasty on Windows, the process just disappears). – Matteo Italia Feb 16 '15 at 10:51
  • @MatteoItalia I have a solution, don't load the data on the stack if you know it's going to be a lot of data, and also don't use windows. But if your data is known not to exceed the stack size, you should avoid using `malloc()`, both, for performance reasons and for simplicity. An image > 8MB is quite a large image, isn't it? it's not so commom except in games and image editing/viewing software perhaps. – Iharob Al Asimi Feb 16 '15 at 10:53
  • @iharob: that's exactly the point; stack allocation is fine for buffers on the small side (typically a few KBs), but you have to be careful when working with data coming from outside. In these cases, even without VLAs, it's customary to have a small stack-allocated buffer with a fallback to `malloc` when stuff gets big. – Matteo Italia Feb 16 '15 at 10:56
  • @iharob: an 8 MB image is nothing special, take a 3 megapixels RGB image and you are already there. – Matteo Italia Feb 16 '15 at 10:59
2

VLA [Variable length array] is a concept present in C99 onwards.

malloc() originates much before that.

Also, malloc() and family allocates memory from heap. It does not use the comparatively limited stack space.

OTOH, gcc allocates space for VLAs in the stack itself.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261