Array size limits

I have a problem with an array that I want to overcome, if I change the value of const int "are" to 2048, the program works fine, but on 8192 or even 4096 (a total of 130,000 elements) it does not work and does not break. How do I get around this?

#include <iostream> #include <fstream> #include <windows.h> #pragma warning (disable : 4820 4619 4668 4101) HANDLE ghEvents; const int arc = 2048; const int are = 8192; struct DataStructure_init { int main_seq[are][32]; int main_seq2[are][32]; int main_seq3[are][32]; int main_lim[are]; }; struct DataStructure_trus { int net[arc]; int r6[arc]; int thr[arc]; }; int ftrus (unsigned char cmain[],int array_inst[],DataStructure_trus& va); int finit (DataStructure_trus va,DataStructure_init& in); using namespace std; int main() { unsigned char cmain[are]; int array_inst[64]={0}; DataStructure_trus va; DataStructure_init in; ftrus(cmain,array_inst,va); finit(va,in); cin.get(); } int finit (DataStructure_trus va,DataStructure_init& in) { int nb=0,flag=0,lock=0; for(int i=0;i<are;i++){ for(int j=0;j<24;j++){ in.main_seq[i][j]=va.thr[(i*24)+j]; } } return 0; } int ftrus (unsigned char cmain[],int array_inst[],DataStructure_trus& va) { int g=0; ifstream in("C:\\Dev-Cpp\\DCS\\Decom\\trus.txt", ios::binary); unsigned char c; while( in.read((char *)&c, 1) ) { cmain[g]=c; if(cmain[g]==' ' && cmain[g-1]=='t' && cmain[g-2]=='e' && cmain[g-3]=='n') {array_inst[1]=g+1;} else if(cmain[g]==' ' && cmain[g-1]=='r' && cmain[g-2]=='h' && cmain[g-3]=='t') {array_inst[9]=g+1;array_inst[21]=g-7;} g++; } array_inst[29]=g-2; for(int i=0;i<64;i++){va.r6[i]=0;} for(int i=array_inst[1];i<array_inst[21];i++){ if(cmain[i]=='1'){va.net[va.r6[1]]=1;va.r6[1]++;} else {va.net[va.r6[1]]=0;va.r6[1]++;} } for(int i=array_inst[9];i<array_inst[29];i++){ if(cmain[i]=='1'){va.thr[va.r6[9]]=1;va.r6[9]++;} else {va.thr[va.r6[9]]=0;va.r6[9]++;} } return 0; } 
+6
source share
5 answers

You do not need to put arrays on the stack in main (), you can also distribute them statically before entering the function. This will put them in an area that is not limited by default stack size.

 unsigned char cmain[are]; int array_inst[64]={0}; DataStructure_trus va; DataStructure_init in; int main() { ftrus(cmain,array_inst,va); finit(va,in); cin.get(); } 
+2
source

Allocate the array dynamically, since there is often a limit on the amount of data you can have on the stack (where local local variables usually occur):

 unsigned char* cmain = new unsigned char[are]; 
+5
source

What everyone else said: you are trying to allocate a lot of things on the stack. Many.

Instead, dynamically allocate a memory buffer ... using a standard container for managing memory:

 std::vector<unsigned char> cmain(are); 
+3
source

You push the data structure onto the stack on main , and it's pretty huge. You can either increase the size of the stack (depending on your system), or select the structure on the heap using new or malloc .

+1
source

You can start by allocating DataStructure_* stack. For example, adding the static .

 static DataStructure_trus va; static DataStructure_init in; 
+1
source

Source: https://habr.com/ru/post/906918/


All Articles