-1

So i write the code.

I dont understand why this is going to EXCEPTION_STACK_OVERFLOW?

#include <iostream>
using namespace std;
int main(){
    char data[2048][2048] = {{0}};
    cout << "test";
    return 0;
}

even if i dont inilitialize

char data[2048][2048];

situation is the same on both cases.

Running "main.exe", press ESC to terminate...        
Crash                                                
  EXCEPTION_STACK_OVERFLOW                           
  time consumed: 0.01 sec                            
  time passed:   0.08 sec                            
  peak memory:   4395008 bytes                       
ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
Daniel
  • 197
  • 2
  • 16

1 Answers1

2

Your variable is too big to keep it on the stack. You should use dynamic storage duration.

std::unique_ptr<char[]> data(new char[2048*2048]);

However, if you really want or must keep it on the stack, here is a gcc flag to change the default stack size:

--stack,4194304 where 4194304 is stack size in bytes

szulak
  • 703
  • 7
  • 16
  • Or you can increase the amount of stack space allocated. – Rob K Mar 26 '15 at 18:25
  • 2
    please do not recommend raw allocation for something that is better solved with a `std::vector` – Mgetz Mar 26 '15 at 18:30
  • @Mgetz I think you should reconsider what did you write in terms of comparison std::vector and std::unique_ptr. – szulak Mar 26 '15 at 18:35
  • @szulak What are you arguing? That thrown exception doesn't leak `data`? That raw pointer isn't harder to use incorrectly than `std::vector` and `std::unique_ptr`? Guess what. It does, and it is. – milleniumbug Mar 26 '15 at 18:56
  • I've shown two options, so OP could choose the one he/she wants. Anyway, I edited my answer and removed raw allocation. – szulak Mar 26 '15 at 18:59