1

I am writing a code to store data from a text file. The code compiles but it seems to stop running in the for-loop in the ReadPanelBlock function. It is structured as: CHBStructure contains CHBPanels which contains CHBOpenings. The functions were created to read from an input text file which I organized into data blocks for easy reading.

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

EDIT 1: I tried tracing where the error occurs by printing text in each line and it seems to crash in the first fscanf function in the ReadPanelBlock function.

1 Answer 1

2

Accessing S->panels[i]->openings[j] is UB, because you did not allocate space for that array.

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

         // inner for
    }

Pre allocated space for openings pointer with:

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

will cause memory leak, because no code will free that memory due to the fact that the address will be lost by the j inner for with code

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

You can avoid to have a pointer and write

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

In your structure CHBPanel *panels[]; and CHBOpening *openings[]; have to be:

CHBPanel *panels; 
CHBOpening *openings;

Last but not least thing: you must study something about pointers and -> and . usage.

Your code should be like:

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;
}
3
  • I tried inserting the malloc line just before the inner for as you suggested but it now reported invalid use of flexible array member. Mar 9, 2016 at 10:57
  • this helps a lot! there are no problems on the syntax but the program still crashes. I've done some checks and it seems the file pointer is not reading the values from the file anymore thus the members of structure openings have 0.0 as values. The program also does not proceed back to the outer loop and stops process on the last iteration of the inner loop. Any thoughts? Thanks again for the help! Mar 9, 2016 at 12:02
  • @CarlChesterRagudo First guess: S->nPanels = 1 and you must check how data are organize into your source file. Post a new question with your code and a sample of file content.
    – LPs
    Mar 9, 2016 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.