1

So you may be asking: "What the heck is a dynamic stack allocation?"

This is what I mean:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
  // this is, I presume, a dynamic stack allocation
  char echoback[strlen(argv[0]) + 1];

  memset(echoback, 0, sizeof(echoback));
  memcpy(echoback, argv[0], strlen(argv[0]));
  printf("%s\n", echoback);  

  return 0;
}

I added the comment "// this is, I presume, a dynamic stack allocation" because I'm actually not sure what assembly code gcc is producing for this. I am not sure if gcc is entirely using stack space for 'echoback' or not.

Presuming that gcc IS putting 'echoback' entirely in stack space, I would call this a "dynamic stack allocation," and my question is, if this is what is happening, is there a gcc command line option to disable this such that a compiler error or warning is produced at compile-time?

i.e., is there something line -fdisable-dynamic-stack or -Wdynamic-stack?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
acker9
  • 363
  • 2
  • 11
  • This is a standard feature of C99. If you don't want it, maybe use C90 or earlier? – Oliver Charlesworth Apr 01 '17 at 19:19
  • https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html -- "Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++." – Roger Lipscombe Apr 01 '17 at 19:19
  • The feature is called "variable length arrays" or VLAs, not "dynamic stack allocation". – Keith Thompson Apr 01 '17 at 19:37
  • 1
    I've closed this as a duplicate. Note that the current accepted answer for the duplicate question doesn't say how to disable VLAs, but the other answer does (use `-Werror=vla`). – Keith Thompson Apr 01 '17 at 19:43
  • `-Werror=vla` is what I was looking for, so I have +1 Keith Thompson's comment. `-Wvla` also works as a warning. – acker9 Apr 01 '17 at 20:25

0 Answers0