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: (
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: (
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).
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 NAESource: https://ru.stackoverflow.com/questions/852970/
All Articles
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