我遇到一个指针,当它传递给一个函数时会改变值...我在各处散布了各种打印语句,确认指针的值没有改变,只是在hashmap中的node-> prev,这导致我丢失列表并使程序崩溃,因为我希望它有效或为0. Print语句指示指针值在传递给pushmap
时发生变化。
我很好奇这是如何可能的:
全部开始:封闭:
RELATION * closure(RELATION * list, RELATION * testdep, DEP_HOLDER * deplist)
{
RELATION *ret = 0;
if(list && testdep && deplist)
{
HASHMAP * top=buildmap(deplist);
ret = copyrelation(testdep);
traversehashmap(ret,top);
}
return ret;
}
buildmap:
HASHMAP * buildmap(DEP_HOLDER * deplist)
{
HASHMAP * ret = 0;
if(deplist)
{
while(deplist)
{
DEPENDENCY *dd = deplist->data;
HASHMAP * nnew = 0;
nnew = createhashmap(dd->left);
if(ret)
{
pushmap(&ret, nnew);
}
else
{
ret = nnew;
}
RELATION *holder = dd->right;
while(holder)
{
RELATION * h = getnewrelation(holder->data);
HASHNODE *node = createhashnode(h);
if(ret->determines)
{
pushnode(&(ret->determines), node);
}
else
{
ret->determines=node;
}
holder=holder->next;
}
deplist=deplist->next;
}
}
return ret;
}
pushmap和pushnode:
void pushmap(HASHMAP **top, HASHMAP **nnew)
{
(*top)->prev=(*nnew);
(*nnew)->next=(*top);
*top=*nnew;
}
void pushnode(HASHNODE **top,HASHNODE *nnew)
{
nnew->next=(*top);
*top=nnew;
}
hashmap和hashnode创建函数以防万一:
HASHMAP * createhashmap(RELATION * value)
{
HASHMAP *ret = (HASHMAP *)malloc(sizeof(HASHMAP *));
ret->value=value; ret->visited=0; ret->prev=0;
ret->next=0; ret->determines=0;
return ret;
}
HASHNODE * createhashnode(RELATION * value)
{
HASHNODE *ret = (HASHNODE *)malloc(sizeof(HASHNODE *));
ret->value=value; ret->next=0;
return ret;
}
编辑:这就是问题所在:
原来这个:
HASHMAP *ret = (HASHMAP *)malloc(sizeof(HASHMAP *));
应该是:
HASHMAP *ret = (HASHMAP *)malloc(sizeof(HASHMAP));
答案 0 :(得分:0)
原来这个:
HASHMAP *ret = (HASHMAP *)malloc(sizeof(HASHMAP *));
应该是:
HASHMAP *ret = (HASHMAP *)malloc(sizeof(HASHMAP));