数组大小限制

发布于 2024-12-29 11:10:44 字数 2174 浏览 0 评论 0原文

我有一个我想克服的数组问题,如果我将 const int "are" 的值更改为 2048,则程序运行良好,但在 8192 甚至 4096(仅 130,000 个元素)时,它无法工作并中断。我该如何解决这个问题?

#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;
}

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) it does not work and breaks. 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

渡你暖光 2025-01-05 11:10:44

动态分配数组,因为堆栈上可以拥有的数据量通常受到限制(这是自动局部变量通常结束的地方):

unsigned char* cmain = new unsigned char[are];

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];
给不了的爱 2025-01-05 11:10:44

其他人都说:你试图在堆栈上分配很多东西。很多

相反,通过使用标准容器进行内存管理来动态分配内存缓冲区:

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

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);
徒留西风 2025-01-05 11:10:44

您不必将数组放在 main() 中的堆栈上,您也可以在进入函数之前静态分配它们。这会将它们置于不受默认堆栈大小限制的区域。

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();
 }  

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();
 }  
星軌x 2025-01-05 11:10:44

您可以从分配不在堆栈中的 DataStructure_* 开始。例如,通过添加 static 关键字。

static DataStructure_trus va; 
static DataStructure_init in;

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;
囍孤女 2025-01-05 11:10:44

您将数据结构放在 main 中的堆栈上,它非常大。您可以增加堆栈大小(取决于您的系统),或者使用 newmalloc 分配堆上的结构。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文