1

Is data allocated on stack inside a function cleared when the function ends? The following code gives me an error (program terminates with 0xC00000FD exception code):

void fun()
{
    const long len = (1024 * 1024) / 4; // 1 MB
    int aa[len]{};
}

int main()
{
    fun();
    const long len = (1024 * 1024) / 4; // 1 MB
    int aa[len]{};
}

If I do not call fun() at the beginning it works fine.

Also, what is a default stack size using MinGW?

michalt38
  • 1,193
  • 8
  • 17
  • *"gives me an error"* - which error? – user7860670 Apr 12 '20 at 12:09
  • The program just terminates with 0xC00000FD exception code. – michalt38 Apr 12 '20 at 12:10
  • 1
    That is a funny one for this site! `0xC00000FD` is a stack overflow! – drescherjm Apr 12 '20 at 12:10
  • Typically one should aim at 4KB stack size (even though it may be much larger). – user7860670 Apr 12 '20 at 12:12
  • Try here for a description of: [0xC00000FD](https://stackoverflow.com/q/18721841/10871073). – Adrian Mole Apr 12 '20 at 12:14
  • I believe the problem is that with a 1MB stack you can't put a single 1MB variable on the stack you need a slightly larger stack. – drescherjm Apr 12 '20 at 12:16
  • it's about compile time and run time constant for array's length you can use direct initialization if you use C++11 const long len {(1024 * 1024) / 4}; or use #define to set len – MH Alikhani Apr 12 '20 at 12:16
  • The question is also about will the memory for array inside function foo be allocated before function foo will be called (in compile time) or when the function gets called? Will the memory for this array be relased when the function will end? Assume that we will use small array, e.g. of 10 elements. – michalt38 Apr 12 '20 at 14:41

0 Answers0