- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
编辑:通过初始化 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
编译这段代码。它编译和运行都很好,没有错误。在将值传递给 grid
后立即使用 printf()
检查值会显示所有正确的值。 然而,代码中的最后一个 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
对我做错了什么有什么想法吗?
最佳答案
i
在这里未初始化使用:
str_array[i] = token;
++i;
注意:
如果您使用符号编译(GCC 的选项 -g
),则 Valgrind 会使用对源代码行的引用来注释其日志记录。
关于c - Valgrind 报告 SIGSEGV 和未初始化值的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114011/
我希望 valgrind 在发现第一个错误时停止并退出。 请勿推荐 --vgdb-error=1 :它不会退出 valgrind。您必须连接 gdb 并从那里终止。 --db-attach : 在最近
有人可以快速解释 Valgrind 的工作原理吗?一个例子:它如何知道内存何时被分配和释放? 最佳答案 Valgrind 基本上在“沙箱”中运行您的应用程序。在此沙箱中运行时,它能够插入自己的指令来进
我有一个因 SIGSEGV 而崩溃的应用程序。 --20183-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) -
我有一个因 SIGSEGV 而崩溃的应用程序。 --20183-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) -
我想使用 valgrind 检查长时间运行的进程是否存在内存泄漏。我怀疑我所追求的内存泄漏可能仅在执行几个小时后才会发生。我可以在 valgrind 下运行应用程序并获取 valgrind 日志,但这
我想用 valgrind 检查一个长时间运行的进程是否有内存泄漏。我怀疑我所追求的内存泄漏可能仅在执行数小时后才会发生。我可以在 valgrind 下运行应用程序并获得 valgrind 日志,但这样
如何在不通过 valgrind 命令选项启动它的情况下对每个 Process 实例执行 valgrind memcheck。 有没有办法将监控选项保存在进程中,而不是每次都使用 valgrind 命令
我使用了“--trace-children=yes”选项,我还使用了“--trace-children-skip=patt1,patt2,...”选项(过滤掉噪音过程)。但它对我来说仍然很慢,我的多进
我从 Valgrind 得到以下日志: MPK ==5263== 4 bytes in 1 blocks are still reachable in loss record 1 of 84 ==52
如何在 Valgrind 抑制文件中添加注释? 我需要为一个大型项目维护一个 Valgrind 抑制文件。我们从我们链接到的工具中过滤无法修复的错误。随着工具的新版本发布,此文件可能需要随着时间的推移
我有一个大程序要运行。使用 valgrind 需要几个小时才能运行。我听说有一些东西可以让我们为程序中的特定函数调用 valgrind。其余程序将正常执行(没有 valgrind env)。 任何人都
我可以用 valgrind 检测整数溢出缺陷吗?里面的哪个工具可以做到这一点? 最佳答案 Valgrind 没有可以检测整数溢出的工具。 您可能会使用 gcc 选项捕获这些错误: -ftrapv Th
我有一个简单的程序: int main(void) { const char sname[]="xxx"; sem_t *pSemaphor; if ((pSemaphor = sem_o
如何让 Valgrind 准确显示错误发生的位置?我编译了我的程序(通过 PuTTy 在 Windows 机器上通过 Linux 终端)添加了 -g 调试选项。 当我运行 Valgrind 时,我得到
或者最好是全部,而不仅仅是我的代码?我的程序使用 Gtk、Loudmouth 和其他一些东西,而这两个(以及它们背后的一些,libgcrypto、libssl)本身导致了如此多的错误,以至于我无法检测
我想尝试使用 valgrind 进行一些堆损坏检测。通过以下腐败“单元测试”: #include #include #include int main() { char * c = (ch
我看过类似的问题here ,但我的问题是我没有编辑 default.supp 文件的权限。例如,Valgrind 中是否有任何忽略所有抑制文件的命令行选项? 最佳答案 在 Valgrind 3.10.
我在一个运行无限循环的程序上使用 valgrind。 由于memcheck在程序结束后显示内存泄漏,但由于我的程序有无限循环,它永远不会结束。 那么有什么方法可以强制从 valgrind 时不时地转储
我一直在尝试使用 valgrind 查找一些可疑的内存错误。 在被分析的程序甚至到达我希望分析的点之前,它会因为对 mmap 的调用开始失败而退出。当它不在 valgrind 下时,这些调用会成功。
由于 OpenSSL 使用未初始化的内存,因此对使用 openldap2 的 libldap 的程序进行 Valgrind 是一件苦差事。存在一个 --ignore-fn选项,但仅适用于 Valgri
我是一名优秀的程序员,十分优秀!