简体   繁体   中英

Segementation fault if accessing struct member in library function

Following Problem: In my main program I declare struct variable and then pass the address to it to a library function (shared-object, compiled by me). The library function should initialize the struct, but it crashes with a segmentation fault. The crash happens when a member (type int) of the struct is set to 0. The problem doesn't happen if I set the same member in the main program.

Main program: C++ (compiled with g++)

rnxctr_t tRNX;
tRNX.ephsat = 0;  // <-- works
init_rnxctr(&tRNX);

Library function: C (compiled with gcc)

extern int init_rnxctr(rnxctr_t *rnx)
{
    gtime_t time0={0};
    obsd_t data0={{0}};
    eph_t  eph0={0,-1,-1};
    geph_t geph0={0,-1};
    seph_t seph0={0};
    int i,j;

    trace(3,"init_rnxctr:\n");

    rnx->obs.data=NULL;
    rnx->nav.eph =NULL;
    rnx->nav.geph=NULL;
    rnx->nav.seph=NULL;

    if (!(rnx->obs.data=(obsd_t *)malloc(sizeof(obsd_t)*MAXOBS ))||
        !(rnx->nav.eph =(eph_t  *)malloc(sizeof(eph_t )*MAXSAT ))||
        !(rnx->nav.geph=(geph_t *)malloc(sizeof(geph_t)*NSATGLO))||
        !(rnx->nav.seph=(seph_t *)malloc(sizeof(seph_t)*NSATSBS))) {
        free_rnxctr(rnx);
        return 0;
    }
    rnx->time=time0;
    rnx->ver=0.0;
    rnx->sys=rnx->tsys=0;
    for (i=0;i<6;i++) for (j=0;j<MAXOBSTYPE;j++) rnx->tobs[i][j][0]='\0';
    rnx->obs.n=0;
    rnx->nav.n=MAXSAT;
    rnx->nav.ng=NSATGLO;
    rnx->nav.ns=NSATSBS;
    for (i=0;i<MAXOBS ;i++) rnx->obs.data[i]=data0;
    for (i=0;i<MAXSAT ;i++) rnx->nav.eph [i]=eph0;
    for (i=0;i<NSATGLO;i++) rnx->nav.geph[i]=geph0;
    for (i=0;i<NSATSBS;i++) rnx->nav.seph[i]=seph0;
    rnx->ephsat=0;    // <-- segmentation fault
    rnx->opt[0]='\0';

    return 1;
}

Struct definition:

typedef struct {        /* rinex control struct type */
    gtime_t time;       /* message time */
    double ver;         /* rinex version */
    char   type;        /* rinex file type ('O','N',...) */
    int    sys;         /* navigation system */
    int    tsys;        /* time system */
    char   tobs[7][MAXOBSTYPE][4]; /* rinex obs types */
    obs_t  obs;         /* observation data */
    nav_t  nav;         /* navigation data */
    sta_t  sta;         /* station info */
    int    ephsat;      /* ephemeris satellite number */
    char   opt[256];    /* rinex dependent options */
} rnxctr_t;

Update : Link to complete header file:rtklib.h

Some observations

The problem only occures if I enable (define) some optional features (DENAGAL, DENACMP) of the library. But the differences to the "normal" version doesn't explain the problem at all. The only thing changing in the posted code is the MAXSAT definition.

Update : I just realized, that a change of MAXSAT does change the size of structs inside rnxctr_t (eg nav_t )

With gdb I can see that the addresses of some members are different in the main program and in the function. Main:

  • &tRNX: 0x7ffffffc6e10
  • &tRNX.nav: 0x7ffffffc7548
  • &tRNX.ephsat: 0x7fffffffd290

Function:

  • rnx: 0x7ffffffc6e10
  • &rnx->nav: 0x7ffffffc7548
  • &rnx->ephsat: 0x800000026ef8

This last point really confuses me, because I don't understand how that can happen.

best regards Michael

I just found the problem. I just realized, that I used two different versions of rtklib.h. Which resulted in the different sizes of the rnxctr_t struct.

So in the end it was just my stupidity, but anyway thanks for your helpful comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM