gpt4 book ai didi

c - 结构处理和结构数组

转载 作者:太空宇宙 更新时间:2023-11-04 02:37:09 25 4
gpt4 key购买 nike

我正在编写代码来存储文本文件中的数据。代码可以编译,但它似乎停止在 ReadPanelBlock 函数的 for 循环中运行。它的结构如下:CHBStructure 包含 CHBPanels,后者包含 CHBOpenings。创建这些函数是为了从输入文本文件中读取,我将其组织成数据 block 以便于阅读。

typedef struct _open
{
int id;
double length;
double height;
double origX;
double origY;
int frames;

double thickness;
double E;
double v;
}CHBOpening;

typedef struct _panels
{
int id;
double length;
double height;
double origX;
double origY;
double origZ;
double angle;
int nOpenings;
int nReinforcement;
double *xReinf;
double sx;
double xReinf0;

CHBUnit *chb;
CHBOpening *openings[];
}CHBPanel;

typedef struct _chb
{
int nStories;
int nModes;
int nIter;
int nPanels;
CHBPanel *panels[];
}CHBStructure;

int ReadPanelBlock (FILE *fp, CHBStructure *S)
{
CHBOpening *openings = malloc(sizeof(CHBOpening));
*S->panels = malloc(S->nPanels*sizeof(CHBPanel));
for (i=0; i<S->nPanels; i++)
{
fscanf(fp,"%d",&S->panels[i]->id);
fscanf(fp,"%lf",&S->panels[i]->length);
fscanf(fp,"%lf",&S->panels[i]->height);
fscanf(fp,"%lf",&S->panels[i]->angle);
fscanf(fp,"%lf",&S->panels[i]->origX);
fscanf(fp,"%lf",&S->panels[i]->origY);
fscanf(fp,"%lf",&S->panels[i]->origZ);
fscanf(fp,"%d",&S->panels[i]->nOpenings);
if (S->panels[i]->nOpenings > 0)
{
for (j=0; j<S->panels[i]->nOpenings;j++)
{
openings = S->panels[i]->openings[j];
fscanf(fp,"%d",&openings->id);
fscanf(fp,"%lf",&openings->length);
fscanf(fp,"%lf",&openings->height);
fscanf(fp,"%lf",&openings->origX);
fscanf(fp,"%lf",&openings->origY);
}
}
}
return 1;
}

编辑 1:我尝试通过在每一行中打印文本来跟踪错误发生的位置,它似乎在 ReadPanelBlock 函数的第一个 fscanf 函数中崩溃。

最佳答案

访问 S->panels[i]->openings[j]UB ,因为您没有为该数组分配空间。

    if (S->panels[i].nOpenings > 0)
{
S->panels[i].openings = malloc(sizeof(CHBPanel)*S->panels[i].nOpenings);

// inner for
}

openings 指针预分配空间:

CHBOpening *openings = malloc(sizeof(CHBOpening));

将导致内存泄漏,因为地址将被 j 内部 for with code 丢失,因此没有代码会释放该内存

openings = S->panels[i]->openings[j];

你可以避免有一个指针而写

        for (j=0; j<S->panels[i].nOpenings;j++)
{
fscanf(fp,"%d",&S->panels[i].openings[j].id);
fscanf(fp,"%lf",&S->panels[i].openings[j].length);
fscanf(fp,"%lf",&S->panels[i].openings[j].height);
fscanf(fp,"%lf",&S->panels[i].openings[j].origX);
fscanf(fp,"%lf",&S->panels[i].openings[j].origY);
}

在你的结构中 CHBPanel *panels[];CHBOpening *openings[]; 必须是:

CHBPanel *panels; 
CHBOpening *openings;

最后但并非最不重要的一件事:你必须研究一些关于指针和->. 用法的东西。

你的代码应该是这样的:

int ReadPanelBlock (FILE *fp, CHBStructure *S)
{
int i,j;
S->panels = malloc(S->nPanels*sizeof(CHBPanel));
for (i=0; i< S->nPanels; i++)
{
fscanf(fp,"%d",&S->panels[i].id);
fscanf(fp,"%lf",&S->panels[i].length);
fscanf(fp,"%lf",&S->panels[i].height);
fscanf(fp,"%lf",&S->panels[i].angle);
fscanf(fp,"%lf",&S->panels[i].origX);
fscanf(fp,"%lf",&S->panels[i].origY);
fscanf(fp,"%lf",&S->panels[i].origZ);
fscanf(fp,"%d",&S->panels[i].nOpenings);
if (S->panels[i].nOpenings > 0)
{
S->panels[i].openings = malloc(sizeof(CHBOpening)*S->panels[i].nOpenings);

for (j=0; j<S->panels[i].nOpenings;j++)
{
fscanf(fp,"%d",&S->panels[i].openings[j].id);
fscanf(fp,"%lf",&S->panels[i].openings[j].length);
fscanf(fp,"%lf",&S->panels[i].openings[j].height);
fscanf(fp,"%lf",&S->panels[i].openings[j].origX);
fscanf(fp,"%lf",&S->panels[i].openings[j].origY);
}
}
}
return 1;
}

关于c - 结构处理和结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35887849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com