【发布时间】:2020-05-08 15:23:13
【问题描述】:
我有一个 30 年前编写的 C 程序(用于 ISAM 文件),其中有一个部分可以帮助我理解/修复。写这篇文章的人看到他/她的 cmets 可能会出现未来的问题。它编译得很好,但核心转储。
这是问题所在:
altkptr = (struct keydat *)(++header);
函数中的代码:
struct indxheader {
int pnumrecs ;
long pnxtposn ;
int pnextnode ;
long pfreedat ;
int pfreenodes;
#ifdef FIXED_RECLEN
int preclength; /* Added 23-Jun-89 .. kavi */
#endif
int paltkeys ;
};
struct keydat {
int pkeylength,
proot,
pmaxkeys,
pnodesize,
pnumkeys,
pkeyparts ,
*partsarray ;
};
static int buildtables(alloc_flg)
int alloc_flg ; /* if occupy slot etc are to be called */
{
struct indxheader *header ;
struct keydat *altkptr;
int savekey, i ;
int *sptr, *dptr ;
savekey = currkeyno ;
if(seek(0) == ERROR)
reterr(NODSKERR);
if(read(currslot.fd2,ptr2,INXHSZ)<INXHSZ)
reterr(IFLRDERR); /* SIZE OF HEADER BLOCK IN FILE < SIZE DECLARED IN ISNAMES.H */
header = (struct indxheader *)ptr2 ;
currslot.numrecords = header->pnumrecs ; /*added .. if i have luck*/
#ifdef FIXED_RECLEN
currslot.reclength = header->preclength ;
#endif
currslot.nxtposn = header->pnxtposn;
currslot.nextnode = header->pnextnode;
currslot.freedat = header->pfreedat;
currslot.freenodes = header->pfreenodes;
currslot.altkeys = header->paltkeys;
/** If occupyslot to be called then check **/
if ( alloc_flg )
if (alloctable() == ERROR) return (ERROR) ;
altkptr = (struct keydat *)(++header); /* deserves attention..*/
for(currkeyno = 0;currkeyno<currslot.altkeys;currkeyno++) {
currindex.proot = altkptr->proot;
if (currindex.proot == -1) reterr (CRPIXFLERR) ;
currindex.pmaxkeys = altkptr->pmaxkeys;
currindex.pnodesize = altkptr->pnodesize;
currindex.pnumkeys = altkptr->pnumkeys;
currindex.pkeyparts = altkptr->pkeyparts ;
currindex.pkeylength = altkptr->pkeylength ;
altkptr++ ;
sptr = (int *)altkptr ;
if (alloc_flg) {
if ((currindex.partsarray = dptr = (int *)malloc((unsigned)(currindex.pkeyparts*4)*sizeof(int))) == NULL) reterr(MEMORYERR) ;
for (i = 0 ; i<(currindex.pkeyparts*4) ; i++)
*dptr++ = *sptr++ ;
}
else sptr += (currindex.pkeyparts*4) ;
altkptr = (struct keydat *)sptr ;
}
currkeyno = savekey ; /*restore currkeyno to its initial value*/
return (NOERROR);
}
【问题讨论】:
-
核心转储时,错误信息是什么?使用您最喜欢的调试器检查核心转储,它会告诉您更多信息。但我看不出
altkptr = (struct keydat *)(++header)是如何导致问题的,因为它是一个简单的指针分配。
标签: c pointers data-structures casting