[英]C: corrupted pointer from push function?
I'm having trouble with a pointer that changes value as its passed to a function… I have various print statements scattered throughout that confirm that the value of the pointer doesn't change, just node->prev in hashmap, which causes me to lose the list and crash the program because I expect it to be valid or 0. Print statements indicate the pointer value changes when its passed to pushmap
. 我遇到一个指针,该指针会在将值传递给函数时更改其值……我在各处散布着各种打印语句,这些语句确认指针的值没有改变,只是散列表中的node-> prev,这导致我丢失列表并使程序崩溃,因为我希望它是有效的或为pushmap
语句指示指针值在传递给pushmap
时发生更改。
I'm curious how this is possible: 我很好奇这怎么可能:
where it all starts: closure: 一切从哪里开始:关闭:
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 and pushnode: 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;
}
the hashmap and hashnode creation functions just in case: 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;
}
EDIT: this was the problem: turn out this: 编辑:这是问题:原来是这样:
HASHMAP *ret = (HASHMAP *)malloc(sizeof(HASHMAP *));
should be: 应该:
HASHMAP *ret = (HASHMAP *)malloc(sizeof(HASHMAP));
turns out this: 事实证明:
HASHMAP *ret = (HASHMAP *)malloc(sizeof(HASHMAP *));
should be: 应该:
HASHMAP *ret = (HASHMAP *)malloc(sizeof(HASHMAP));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.