Valgrind… 4 bytes inside a block of size 8 free'd

本秂侑毒 提交于 2019-12-01 03:37:05

As caf already wrote, you're accessing memory that has just been freed.

To fix that, just don't use double pointers, single pointers will do very well here.

So replace

lnode **nptr = &s->head;

by

lnode *nptr = s->head;

Same for

lnode **tmp = nptr;

in the loop. Make it

lnode *tmp = nptr;

and drop the double assignment just when you at it.

Then access value and next by

tmp->value

and

tmp->next

directly

In each iteration other than the first, tmp points at the next pointer from the previous node - but you've already freed that node (in the previous iteration), so tmp points into a freed block and you can't dereference it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!