【发布时间】:2016-03-06 11:41:02
【问题描述】:
下午好!这是我在这里的第一篇文章!
我在使用 valgrind 时遇到了无效的写入错误,但是当我使用 gdb 时可以解决!
#include <stdio.h>
#include <stdlib.h>
#define MAX_INDEX 2
void *z_m = 0;
struct block {
struct block * suiv;
};
//Declaration of a global array
struct block * tzl[MAX_INDEX+1];
//Function used to dislay tzl in the main.
void display() {
for(int i=0; i<=MAX_INDEX; i++) {
struct bloc * tmp = tzl[i];
printf("%d => ",i);
while (tmp!=NULL) {
printf(" %li ->",(unsigned long)tmp);
tmp = tmp -> suiv;
}
printf("\n");
}
}
int main() {
z_m = (void *) malloc(1<<MAX_INDEX);
for (int i=0; i<MAX_INDEX; i++)
{
tzl[i] = NULL;
}
tzl[MAX_INDEX] = z_m;
//Here is the problem with valgrind
tzl[MAX_INDEX] -> suiv = NULL;
display();
free(z_m);
return 0;
}
可能是什么问题?谢谢你的回答。
【问题讨论】:
-
您能否解释(或改写)“但是当我使用 gdb 时我什么时候能弄明白”这句话?
-
顺便说一句,您的符号(函数和变量)名称实际上是不可读的(至少对于普通的英文读者来说,这是期望在此处发布问题的方式)。
-
对不起!我尝试添加一些 cmets。当我使用 gdb 时,进程正常终止,考虑到 valgrind 中的错误,这对我来说很奇怪!
-
Tank you!?! ;-)
-
@Cool Guy:这只是一个错字!