Bas*_*sen 0 c valgrind pointers
我有一个包含一些元素的结构,我在循环中释放了这个结构的内存,大致如下:
for (i = 0; i < teller; i++) {
free((glycan+i)->desc);
}
free(glycan)
Run Code Online (Sandbox Code Playgroud)
我假设指针仍然指向空的内存块,因此我想将它们设置为NULL,如下所示:
for (i = teller; i > 0; i--) {
(glycan+i)->desc = NULL;
}
glycan = NULL;
Run Code Online (Sandbox Code Playgroud)
然而Valgrind告诉我一些我不太懂的东西:
==11783== Invalid write of size 4
==11783== at 0x8048F49: main (spectral_matcher.c:122)
==11783== Address 0x431c070 is 72 bytes inside a block of size 28,000 free'd
==11783== at 0x4027C02: free (vg_replace_malloc.c:366)
==11783== by 0x8048F2C: main (spectral_matcher.c:121)
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我解释为什么会发生这种警告/错误以及我应该采取哪些不同的方法来解决它?
编辑:我知道我在释放后将指针设置为NULL,释放只将内存标记为空闲,因此指针仍然完整(如果我没有记错),我随后希望将其设置为NULL.
一旦你释放变量,glycan你就不能再触摸(glycan+i)->desc了 - 也没有意义.
关于感觉部分:想一想,如果你说glycan = NULL,为什么你会关心个别元素的成员?