Array size limits

后端 未结 5 583
北海茫月
北海茫月 2021-01-12 17:41

I have an array problem that i want to overcome, if i change the value of const int \"are\" to 2048 the program runs fine but at 8192 or even at 4096 ( just 130,000 elements

相关标签:
5条回答
  • 2021-01-12 17:50

    What everyone else said: you're trying to allocate a lot of stuff on the stack. A lot.

    Instead, dynamically-allocate the memory buffer... by using a standard container for memory management:

    std::vector<unsigned char> cmain(are);
    
    0 讨论(0)
  • 2021-01-12 17:57

    You don't have to put the arrays on the stack in main(), you can just as well allocate them statically before entering the function. That will put them in an area that is not limited by the 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();
     }  
    
    0 讨论(0)
  • 2021-01-12 17:58

    Allocate the array dynamically, since there are often limits on how much data you can have on the stack (which is where automatic local variables typically end up):

    unsigned char* cmain = new unsigned char[are];
    
    0 讨论(0)
  • 2021-01-12 18:08

    You can start with allocating DataStructure_* not in the stack. For instance by prepending the static keyword.

    static DataStructure_trus va; 
    static DataStructure_init in;
    
    0 讨论(0)
  • 2021-01-12 18:12

    You are putting the data structure on the stack in main, and it is pretty huge. You can either increase the stack size (depends on your system), or allocate the structure on the heap with new or malloc.

    0 讨论(0)
提交回复
热议问题