Why is that?

bool arr[256][4096]; arr[20][100] = true; cout << arr[20][101]; // Stack overflow 

VS2015, a test on online compilers does not reveal an error: (

  • four
    Do not declare stack variables of large sizes. You have a variable in the function, the function in recursion is not exactly included? Well, let's not included. But in order to understand how much the stack has eaten ... you can see either the esp difference or review all local variables in the stack windows. - nick_n_a
  • one
    that is another question. this is indicated in the linker parameters. You have a studio - msdn.microsoft.com/ru-ru/library/8cxs58a6.aspx in the build options indicates the stack size. This size can be increased ... but I advise you to either end up with large sizes or declare with statics, or do malloc / new. - nick_n_a
  • one
    The studio has a default stack size (for 32bit) - 1 MB. And you just take 1Mb and pick it up, and then another 20-50 stack windows for which you will also need a 1-2 KB stack. - nick_n_a
  • one
    @Robert if you have not encountered them - does not mean that they are not there. Just by default the stack size is bigger there. As I recall - vegorov
  • one
    @Robert, see what a thing, if an array of flags is a memory-critical thing, then it makes sense to store each flag in bits: 256 * 65536/8 = 2MB std::vector<bool> does this. The array as you will take 16MB as bool in this case takes 1 byte. To do a vector of vectors is a waste of memory on pointers, and throw a problem into the file. Want to simplify use - make the display of a two-dimensional array in one-dimensional: std :: vector <unsigned char> flags (256 * 65536) ... bool flag (int setting, int group) {return flags [65536 * group + setting];} - mr NAE

1 answer 1

If you want a matrix of fixed bool sizes:

 #include <memory> ... typedef bool t_arr[256][4096]; std::unique_ptr<t_arr> p_arr(new t_arr); (*p_arr)[20][100] = true; t_arr& arr=*p_arr; // эту ссылку используем, если лениво каждый раз разыменовывать указатель. arr[20][100] = true; cout << arr[20][101]; 

for modern compilers can be more concise:

 auto p_arr=std::make_unique< bool[256][4096] >(); auto& arr=*p_arr; arr[20][100] = true; cout << arr[20][101]; 

Another way ("More C ++ - nd"), standard containers:

 #include <vector> ... std::vector< std::vector< bool > > arr( 256, std::vector< bool >( 4096 ) ); arr[20][100] = true; cout << arr[20][101]; 

The disadvantage of the second approach is that there is no solid field of memory (several 257 allocations will be made, instead of one, and the code will turn out less processor-friendly). Advantage: less memory will be used (due to the specialized std :: vector <bool>, as an array of bit flags, not bool s).

  • Container unique_ptr will free up memory when leaving function? - nick_n_a
  • one
    Yes. Only this is a "smart pointer", not a container. He will call delete in his destructor, - Chorkov
  • one
    All options are pretty with ++. It is quite possible here without std :: unique_ptr: t_arr *p_arr = new t_arr and somewhere delete p_arr; But this is not the answer to the original question, but the answer: ru.stackoverflow.com/questions/623160/… - mr NAE