编辑:通过初始化i
解决的所有valgrind错误。
我正在处理琴弦;从文件中读取字符串,使用strsep()
进行分隔,然后使用atol()
将其转换为int。我的代码在gcc上编译并运行,没有错误,但是用valgrind检查时,出现标题中提到的错误。
我正在尝试使用文本文件中的int值填充二维数组grid[20][20]
。文本文件包含用空格分隔的数字行,例如20 81 65 07 54 72 13 28 66 95 00 20 00 84 06 30 85 43 15 73\n
我的代码(不包括所有文件IO)如下。 (请注意,我确实正确处理了文件:检查fopen
的结果,并以fclose
关闭)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR_LEN 20
#define NUM_LINES 20
#define MAX_SIZE 100
int main(void){
char input_str[MAX_SIZE]; //string to hold lines read from file
char * str_array[STR_LEN]; //array of strings to split the lines into
int grid[20][20];
char * token, *str, *tofree;
int line;
for (line = 0; line < NUM_LINES; ++line){ //for every line in the file
if (fgets(input_str, MAX_SIZE, fp)!=NULL){ //read the file's line into input_str
tofree = str = strdup(input_str); //set str to copy of input_string, and tofree to point to beginning of str's memory, to free later
int i = 0;
while ((token = strsep(&str, " "))){ //split str on " ", keeping everything in between in token
str_array[i] = token; //add token to str_array
++i;
}
for (i = 0; i < STR_LEN; ++i){
grid[line][i] = atol(str_array[i]); //convert strings to int and store in num_array
printf("grid[%d][%d]: %d\n", line, i, grid[line][i]);
}
free(tofree); //free str by freeing tofree, as tofree points to str's memory
}
}
printf("%d", grid[0][0]);
return 0;
}
我用gcc -Wall -g -c myfile.c
编译此代码。它既编译又运行良好,没有错误。将值传递到printf()
之后立即使用grid
检查值将显示所有正确的值。 但是,代码中的最后一个printf()
显示grid[0][0]
包含垃圾,即使以前是正确的。实际上,grid[0]
中的所有元素都已损坏。
使用valgrind --leak-check=full --track-origins=yes --dsymutil=yes --show-leak-kinds=all ./productGrid.elf
运行valgrind给我
$ valgrind --leak-check=full --track-origins=yes --dsymutil=yes --show-leak-kinds=all ./productGrid.elf
==2252== Command: ./productGrid.elf
==2252==
==2252== Use of uninitialised value of size 4
==2252== at 0x106EC: main (in /home/jesse/C/Proj. Euler/productGrid.elf)
==2252== Uninitialised value was created by a stack allocation
==2252== at 0x10656: main (in /home/jesse/C/Proj. Euler/productGrid.elf)
==2252==
==2252== Invalid write of size 4
==2252== at 0x106EC: main (in /home/jesse/C/Proj. Euler/productGrid.elf)
==2252== Address 0x7e2b6c38 is not stack'd, malloc'd or (recently) free'd
==2252==
==2252==
==2252== Process terminating with default action of signal 11 (SIGSEGV)
==2252== Access not within mapped region at address 0x7E2B6C38
==2252== at 0x106EC: main (in /home/jesse/C/Proj. Euler/productGrid.elf)
==2252== If you believe this happened as a result of a stack
==2252== overflow in your program's main thread (unlikely but
==2252== possible), you can try to increase the size of the
==2252== main thread stack using the --main-stacksize= flag.
==2252== The main thread stack size used in this run was 8388608.
==2252==
==2252== HEAP SUMMARY:
==2252== in use at exit: 413 bytes in 2 blocks
==2252== total heap usage: 3 allocs, 1 frees, 4,509 bytes allocated
==2252==
==2252== 61 bytes in 1 blocks are still reachable in loss record 1 of 2
==2252== at 0x483E380: malloc (vg_replace_malloc.c:299)
==2252== by 0x48E188B: strdup (strdup.c:42)
==2252== by 0x106C9: main (in /home/jesse/C/Proj. Euler/productGrid.elf)
==2252==
==2252== 352 bytes in 1 blocks are still reachable in loss record 2 of 2
==2252== at 0x483E380: malloc (vg_replace_malloc.c:299)
==2252== by 0x48D11F3: __fopen_internal (iofopen.c:69)
==2252== by 0x10681: main (in /home/jesse/C/Proj. Euler/productGrid.elf)
==2252==
==2252== LEAK SUMMARY:
==2252== definitely lost: 0 bytes in 0 blocks
==2252== indirectly lost: 0 bytes in 0 blocks
==2252== possibly lost: 0 bytes in 0 blocks
==2252== still reachable: 413 bytes in 2 blocks
==2252== suppressed: 0 bytes in 0 blocks
==2252==
==2252== For counts of detected and suppressed errors, rerun with: -v
==2252== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 3)
Segmentation fault
关于我在做什么错的任何想法吗?
答案 0 :(得分:2)
if __name__ ==
在这里未初始化使用:
i
注意:
如果使用符号(GCC的选项 str_array[i] = token;
++i;
进行编译),则Valgrind会使用对源代码行的引用来注释其日志记录。
答案 1 :(得分:1)
我无法发表评论,所以我发布了一个新答案。正如@alk所说,i
尚未初始化,如果仅在定义时对其进行初始化,则while无效。根据需要使用for(;;)
。
如果仍然有问题,请更新代码。
答案 2 :(得分:1)
您应该在开始时初始化i。
关于这段代码:
while ((token = strsep(&str, " "))){
str_array[i] = token; //add token to str_array
++i;
}
for (i = 0; i < STR_LEN; ++i){
grid[line][i] = atol(str_array[i]);
printf("grid[%d][%d]: %d\n", line, i, grid[line][i]);
}
如果要在下面将i归零,为什么还要计算i?也许您应该使用一个不同的计数器并循环直到此而不是STR_LEN。
希望这会有所帮助。