[英]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.
当结构的成员(int 类型)设置为 0 时会发生崩溃。如果我在主程序中设置相同的成员,则不会发生问题。
Main program: C++ (compiled with g++)主程序:C++(用g++编译)
rnxctr_t tRNX;
tRNX.ephsat = 0; // <-- works
init_rnxctr(&tRNX);
Library function: C (compiled with gcc)库函数:C(用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更新:链接到完整的头文件:rtklib.h
Some observations一些观察
The problem only occures if I enable (define) some optional features (DENAGAL, DENACMP) of the library.仅当我启用(定义)库的某些可选功能(DENAGAL、DENACMP)时才会出现此问题。 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.发布的代码中唯一改变的是
MAXSAT
定义。
Update : I just realized, that a change of MAXSAT
does change the size of structs inside rnxctr_t
(eg nav_t
)更新:我刚刚意识到,那的变化
MAXSAT
不改变内部结构的大小rnxctr_t
(如nav_t
)
With gdb I can see that the addresses of some members are different in the main program and in the function.用gdb可以看到主程序和函数中一些成员的地址不同。 Main:
主要的:
Function:功能:
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.
我刚刚意识到,我使用了两个不同版本的 rtklib.h。 Which resulted in the different sizes of the rnxctr_t struct.
这导致了 rnxctr_t 结构的不同大小。
So in the end it was just my stupidity, but anyway thanks for your helpful comments.所以最后这只是我的愚蠢,但无论如何感谢您的有用评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.