1

My supervisor saw something in my C code similar to:

size = f(some parameters);
int array[size];

He said that it is preferable to allocate such arrays with malloc. His description was something like:

Allocating it on the stack like that requires that your stack remains valid for the entire run-time of the program.

I had no clue what he meant, so this is just as close as I remember to his wording by the end of the meeting, without knowing the actual meaning. What did he mean? (alternatively, maybe someone can explain another reason not to declare arrays in such manner).

Alex
  • 947
  • 1
  • 8
  • 25
  • You have a good memory. Locally allocated arrays don't :) Once you leave the function the memory allocated by such a definition will be freed. It is not the case with `malloc`ed memory, which is remaining allocated until you manually `free` it. But there are usecases for each one of these, you can't say one is better than the other/ – Eugene Sh. May 23 '17 at 16:41
  • If you don't leak `array` outside of the function, there's no lifetime worry, but another important issue is that there's no way to handle errors if the size causes a stack overflow. – Ry- May 23 '17 at 16:41
  • 1
    There is almost certainly a duplicate somewhere, but basically, a local variable can be allocated on the stack, but the function that defined it must remain active, and all other function calls that refer to the variable must be made from this function (or from *those* functions called). When the function that declared the local variable exits, the variable goes out of scope and life. Also, the size of memory available on the stack is limited in comparison with the amount of memory available on the heap. – Weather Vane May 23 '17 at 16:44
  • 1
    @Alex [this previous question](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) might be a good read. – Weather Vane May 23 '17 at 16:54

2 Answers2

7

Any variable declared local will be only valid during the execution of the function, as it will be allocated in stack

If you need this array outside the function where it was declared you need to allocate it with malloc, but if you are using it only inside this function and during this execution is okay

Just mind that any local variables will use stack memory, so be sure that your array will never be big enough to cause a stack overflow in your program!

Gustavo Laureano
  • 556
  • 2
  • 10
  • So if I declare it like that in main(), then I should be fine? – Alex May 23 '17 at 16:43
  • @Alex unless it is too big to fit in the stack, which is pretty limited. – Eugene Sh. May 23 '17 at 16:44
  • If there's a stack overflow (lol, our site), then the program will just crash? – Alex May 23 '17 at 16:44
  • 1
    @Alex it will. Or it won't. Or it will sometimes. Or it cause some weird stuff. It is undefined. – Eugene Sh. May 23 '17 at 16:44
  • OK, thanks. Talk about undefined behaviour, SO doesn't allow me to accept your answer yet... Question: so there's no way to check for stack overflow? – Alex May 23 '17 at 16:45
  • @Alex that is probably to allow time for other answers to be posted. – Weather Vane May 23 '17 at 16:46
  • 1
    @Alex Stack overflow is when your stack grow beyond the region allocated to it, so it can start overwriting other variables, program code, or even crash the system due to unmapped memory access.. thats why we say "undefined behavior", basically anything can happen, your code can just run fine, or can start doing things that was never programmed to do, this is one classic way to exploit systems – Gustavo Laureano May 23 '17 at 16:47
  • @EugeneSh. "Stack space is limited" - Actually not so much, or rather, depends - On a lot of systems, stack and heap are the same memory, just consumed from different ends. So, the same limits apply to both. – tofro May 23 '17 at 17:14
3

There's nothing wrong with using variable length arrays in in general. The array will live in the scope it is declared in, just like any other variable. This is something you should always have in mind, so of course, if you declare the VLA in a function and attempt to return a pointer to it, this is an error (it doesn't exist any more once you leave the function).

Reasons not to use VLAs:

  • If you need the array even after returning from the function that creates it, of course you can't use it, as explained above, but this is true for any object with automatic storage
  • Stack space is somewhat limited, so if your array will be really big, it's better to dynamically allocate it.
  • makes VLAs optional. So your code using VLAs might not work any more on a standard-compliant C11 compiler.
  • What's VLA? Just trying to figure out what exactly won't work in C11. – Alex May 23 '17 at 16:47
  • **V**ariable **L**ength **A**rray -- it's optional in C11, so a compiler can leave that feature out and still conform to C11 –  May 23 '17 at 16:47
  • @FelixPalmen Can you please give a reference to the paragraph making it optional? – Eugene Sh. May 23 '17 at 16:51
  • 2
    6.7.6.2 4 *(Variable length arrays are a conditional feature that implementations need not support; see 6.10.8.3.)* –  May 23 '17 at 16:59
  • @EugeneSh. You know that, if you don't put an @ on your comment, then the answer poster (in this case, Felix) is always notified? So you didn't have to put the @ FelixPalmen this time. Though he did need to notify you, but forgot. – Alex May 23 '17 at 16:59
  • @Alex I know it. But it is for visual reference as well. We have more than two people in this conversation, so it is better to explicitly let people know who you are talking to. – Eugene Sh. May 23 '17 at 17:01