[Bug c/28954] New: Get unexpected segment error

lionghostshop at yahoo dot com dot cn gcc-bugzilla@gcc.gnu.org
Tue Sep 5 11:53:00 GMT 2006


I define a struct array called 'l' in above the main file. Then I passed 'l' to
load_data(struct line l[]) which is defined in another file called load.c. I
declare load_data(struct line l[]) in load.h.

 Then when load_data() want to initialize the l array, a segmentation is
generated.(I have checked that the array is large enough, and I initialized
some elements in l, and printf them out, the value is what I did. But
segmentation error came up as long as I modify the array l).

 Then if I renamed l as ll in main file above. Everything worked well then. Or
if I use externs, every thing worked well as well.

main.c

#include<stdio.h>
#include<linux/compiler.h>
#include"./type.h"
#include"./load.h"
struct line ll[LMAX];
struct machine m[MMAX];
int
main()
{
        //int i;
        printf("Loading data ...");
        fflush(stdout);
        ll[0].co[0]=54321;
        if(-1==load_data(ll))
        {
                printf("Missing data. Exiting program.\n");
                return 0;
        }
        printf(" [OK] \n");

        return 0;
}


load_data.c
#include"./type.h"
#include<stdio.h>
#include<linux/compiler.h>

extern struct machine m[MMAX];
//extern struct line    l[LMAX];
struct line *l;

FILE *
file_open(const char name[])
{
        FILE *f=fopen(name,"r");
        if(unlikely(NULL==f))
        {
                f=fopen(name,"a+");
                fclose(f);
                return NULL;
        }
        return f;
}
int
load_equation()
{       
        int i,j;
        FILE *f=file_open("equation");
        printf("before critical ");
        printf("before critical %lf\n",l[0].co[0]);
        l[0].co[0]=4444;
        printf("critical\n");
        if(NULL==f)
                return -1;
//      printf("before loop\n");
        for(j=0;j<LMAX;j++)
                for(i=0;i<MMAX;i++)
                {
                        printf("before in\n");
                        l[j].co[i]=1;
                        printf("after in\n");
                }
        fclose(f);
        return 0;
}
int 
load_line_limit()
{
        FILE *f=file_open("line_limit");
        int i;
        if(NULL==f)
                return -1;
        for(i=0;i<LMAX;i++)
                fscanf(f,"%lf",&l[i].normal);
        for(i=0;i<LMAX;i++)
        {
                fscanf(f,"%lf%%",&l[i].high);
                l[i].high=(l[i].high/100+1)*l[i].normal;
        }
        fclose(f);
        return 0;
}
int 
load_seg_prise()
{
        FILE *f=file_open("seg_prise");
        int i,j;
        if(NULL==f)
                return -1;
        for(j=0;j<MMAX;j++)
                for(i=0;i<SEGMAX;i++)   
                        fscanf(f,"%lf",&m[j].seg_prise[i]);
        fclose(f);
        return 0;
}
int 
load_seg_len()
{
        FILE *f=file_open("seg_len");
        int i,j;
        if(NULL==f)
                return -1;
        for(j=0;j<MMAX;j++)
                for(i=0;i<SEGMAX;i++)
                {
                        fscanf(f,"%lf",&m[j].seg[i]);
                        if(i)
                                m[j].seg[i]+=m[j].seg[i-1];
                }

        fclose(f);
        return 0;
}
int
load_speed()
{
        FILE *f=file_open("speed");
        if(!f)
                return -1;
        int i;
        for(i=0;i<MMAX;i++)
                fscanf(f,"%lf",&m[i].speed);
        return 0;
}
int
load_data(struct line *_l)
{
        int re=0;       
        l=_l;
        printf("\n>>>0 %p %p %lf\n",l,m,l[0].co[0]);
        l[0].co[0]=12345;
        printf("after\n");
        if(load_equation())
                re=-1;
        if(load_line_limit())
                re=-1;
        if(load_seg_prise())
                re=-1;
        if(load_seg_len())
                re=-1;
        if(load_speed())
                re=-1;
        return re;
}


type.h
#ifndef TYPE
#define MMAX 8
#define LMAX 6
#define SEGMAX 10
struct line{
        double co[MMAX];
        double normal;
        double high;
};
struct machine{
        double seg_prise[SEGMAX];
        double seg[SEGMAX];
        double speed;//climb speed, mw per minute
};
struct solution{
        double m_power[MMAX];   
};
#endif


-- 
           Summary: Get unexpected segment error
           Product: gcc
           Version: 3.4.6
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: lionghostshop at yahoo dot com dot cn


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28954



More information about the Gcc-bugs mailing list