void readdat (int c, char **v) {
char *dc;
char *pdc;
dc = malloc((line+1) * sizeof(char));
memset(dc, 0, (line+1) * sizeof(char));
FILE *datfile;
datfile = fopen(v[3], "r");
while(fgets(dc, line, datfile) != NULL) {
pdc = strtok(dc, "\t");
countcDat = 0;
while(pdc != NULL) {
++countcDat;
pdc = strtok(NULL, "\t");
}
++countrDat;
}
dat = malloc(countrDat * sizeof(char**));
memset(dat, 0, countrDat * sizeof(char**));
for(i=0;i<countrDat;++i) {
dat[i] = malloc(countcDat * sizeof(char*));
memset(dat[i], 0, countcDat * sizeof(char*));
}
for(i=0;i<countrDat;++i) {
for(j=0;j<countcDat;++j) {
dat[i][j] = malloc(20 * sizeof(char));
memset(dat[i][j], 0, 20 * sizeof(char)); ###
}
}
rewind(datfile);
countrDat = 0;
countcDat = 0;
while(fgets(dc, line, datfile) != NULL) {
pdc = strtok(dc, "\t");
countcDat = 0;
while(pdc != NULL) {
sscanf(pdc, "%s", dat[countrDat][countcDat]);
++countcDat;
pdc = strtok(NULL, "\t");
}
++countrDat;
}
for(i=0;i<countrDat;++i) {
for(j=0;j<countcDat;++j) {
printf("%s\t", dat[i][j]);
}
printf("\n");
}
fclose(datfile);
free(dc);
for(i=0;i<countrDat;++i) {
for(j=0;j<countcDat;++j) {
free(dat[i][j]);
}
}
for(i=0;i<countrDat;++i) {
free(dat[i]);
}
free(dat);
}
valgrind 说在 memset(“###” - 标记行)处写入大小为 1 的无效。请帮助找出这里出了什么问题。在 valgrind 输出的“地址 0x0 不是堆栈,也不是 malloc'd 或 free'd”之后,我可以排除 strtok 负责。这大概是显而易见的……