- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我正在处理的程序上运行 valgrind,但出现以下错误 - 我不明白为什么。提前提供任何帮助将不胜感激。如果您需要更多信息,请告诉我
==27928== Invalid read of size 1
==27928== at 0x4EB9390: __GI___rawmemchr (rawmemchr.S:25)
==27928== by 0x4EA514F: _IO_str_init_static_internal (strops.c:45)
==27928== by 0x4E86D64: __isoc99_vsscanf (isoc99_vsscanf.c:42)
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33)
==27928== by 0x401A76: countAndSort (lab1.c:442)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928== Address 0x55bb040 is 0 bytes inside a block of size 10,000,000 free'd
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427)
==27928== by 0x4019D6: countAndSort (lab1.c:430)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928==
==27928== Invalid read of size 1
==27928== at 0x4C2B834: __GI___rawmemchr (mc_replace_strmem.c:1110)
==27928== by 0x4EA514F: _IO_str_init_static_internal (strops.c:45)
==27928== by 0x4E86D64: __isoc99_vsscanf (isoc99_vsscanf.c:42)
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33)
==27928== by 0x401A76: countAndSort (lab1.c:442)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928== Address 0x55bb041 is 1 bytes inside a block of size 10,000,000 free'd
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427)
==27928== by 0x4019D6: countAndSort (lab1.c:430)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928==
==27928== Invalid read of size 1
==27928== at 0x4E7D920: _IO_vfscanf (vfscanf.c:620)
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44)
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33)
==27928== by 0x401A76: countAndSort (lab1.c:442)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928== Address 0x55bb040 is 0 bytes inside a block of size 10,000,000 free'd
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427)
==27928== by 0x4019D6: countAndSort (lab1.c:430)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928==
==27928== Invalid read of size 1
==27928== at 0x4EA36D2: _IO_sputbackc (genops.c:731)
==27928== by 0x4E7D97B: _IO_vfscanf (vfscanf.c:625)
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44)
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33)
==27928== by 0x401A76: countAndSort (lab1.c:442)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928== Address 0x55bb040 is 0 bytes inside a block of size 10,000,000 free'd
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427)
==27928== by 0x4019D6: countAndSort (lab1.c:430)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928==
==27928== Invalid read of size 1
==27928== at 0x4E7DC7D: _IO_vfscanf (vfscanf.c:1394)
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44)
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33)
==27928== by 0x401A76: countAndSort (lab1.c:442)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928== Address 0x55bb040 is 0 bytes inside a block of size 10,000,000 free'd
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427)
==27928== by 0x4019D6: countAndSort (lab1.c:430)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928==
==27928== Invalid read of size 1
==27928== at 0x4E7E9EF: _IO_vfscanf (vfscanf.c:1781)
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44)
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33)
==27928== by 0x401A76: countAndSort (lab1.c:442)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928== Address 0x55bb041 is 1 bytes inside a block of size 10,000,000 free'd
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427)
==27928== by 0x4019D6: countAndSort (lab1.c:430)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928==
==27928== Invalid read of size 1
==27928== at 0x4EA36D2: _IO_sputbackc (genops.c:731)
==27928== by 0x4E7DDF3: _IO_vfscanf (vfscanf.c:1811)
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44)
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33)
==27928== by 0x401A76: countAndSort (lab1.c:442)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928== Address 0x55bb041 is 1 bytes inside a block of size 10,000,000 free'd
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427)
==27928== by 0x4019D6: countAndSort (lab1.c:430)
==27928== by 0x4014EB: doProcess (lab1.c:316)
==27928== by 0x4011C2: main (lab1.c:211)
==27928==
无论您对每个错误的起源有何看法,通过大量的试验和错误,在代码块中注释掉/返回并在测试编译中单独测试代码段,我发现以下是错误的来源。如果我在程序中注释掉这段代码,所有错误都会消失。
注意:整数是全局 int* 类型。 countToUse 是一个全局 int 类型。这里代码的目标是从 stdin 读取所有整数并将它们放入正确大小的动态数组中。
// Load stdin into a buffer
char *buffer = malloc(BUFFER_SIZE);
if(buffer == NULL){
fprintf(stderr, "Malloc for stdin buffer in countAndSort() failed. Exiting.\n");
exit(1);
}
if (fgets(buffer, BUFFER_SIZE, stdin) == NULL) {
fprintf(stderr, "Error loading input from stdin into buffer. Exiting.\n");
exit(1);
}
// Get a count of the numbers to create the array
int count = 0;
int index = 0;
int num, delta;
while (index < BUFFER_SIZE && sscanf(&buffer[index], "%d%n", &num, &delta) == 1){
count++;
index += delta;
}
// Initialize the array with the proper size
integers = malloc(count*sizeof(int));
if(integers == NULL){
fprintf(stderr, "Malloc for integer array in countAndSort() failed. Exiting. \n");
exit(1);
}
// Load the integers into the array
index = 0;
for (int i = 0; i < count; i++){
// LINE 442 IS RIGHT BELOW THIS LINE ------------------------------------
if (index < BUFFER_SIZE && sscanf(&buffer[index], "%d%n", &integers[i], &delta) != 1){
fprintf(stderr, "There was a problem reading the buffer!\n");
exit(1);
}
index += delta;
}
if(buffer != NULL)
free(buffer);
countToUse = count;
最佳答案
您需要确保读取的内容没有超出缓冲区
的范围,如下所示
while ((index < 10000000) && (sscanf(&buffer[index], "%d%n", &num, &delta) == 1))
这同样适用于所有buffer[index]
。
此外,从错误来看,您似乎有 free()
d buffer
,然后尝试从中读取。
关于c - 从 stdin 获取输入的问题 - 来自 valgrind 的读/写无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28467525/
我希望 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
我是一名优秀的程序员,十分优秀!