Valgrind的报告错误一个非常简单的C程序 [英] Valgrind reports errors for a very simple C program
问题描述
我学习C语言从学习C艰难的历程。我在锻炼6 ,虽然我可以让它工作,Valgrind的repots很多误区
I'm learning C language from Learn C The Hard Way. I'm on exercise 6 and while I can make it work, valgrind repots a lot of errors.
这里的剥离下来最少程序从一个文件 ex6.c
:
Here's the stripped down minimal program from a file ex6.c
:
#include <stdio.h>
int main(int argc, char *argv[])
{
char initial = 'A';
float power = 2.345f;
printf("Character is %c.\n", initial);
printf("You have %f levels of power.\n", power);
return 0;
}
内容的Makefile
就是 CFLAGS = -Wall -g
。
我编译 $程序使EX6
(没有编译器警告或错误)。与 $ ./ex6
执行产生预期的输出。
I compile the program with $ make ex6
(there are no compiler warnings or errors). Executing with $ ./ex6
produces the expected output.
当我运行 $的valgrind ./ex6
程序我得到我解决不了的错误。下面是完整的输出:
When I run the program with $ valgrind ./ex6
I get errors which I can't solve. Here's the full output:
==69691== Memcheck, a memory error detector
==69691== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==69691== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==69691== Command: ./ex6
==69691==
--69691-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--69691-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--69691-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
==69691== Conditional jump or move depends on uninitialised value(s)
==69691== at 0x1003FBC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==69691== by 0x1001EFBB6: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001FA005: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021F9CE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021FCA0: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F5B91: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F39F7: printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x100000F1B: main (ex6.c:8)
==69691==
Character is A.
==69691== Invalid read of size 32
==69691== at 0x1003FBC1D: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==69691== by 0x1001EFBB6: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001FA005: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021F9CE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021FCA0: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F5B91: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F39F7: printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x100000F31: main (ex6.c:9)
==69691== Address 0x100809680 is 32 bytes before a block of size 32 in arena "client"
==69691==
You have 2.345000 levels of power.
==69691==
==69691== HEAP SUMMARY:
==69691== in use at exit: 39,365 bytes in 429 blocks
==69691== total heap usage: 510 allocs, 81 frees, 45,509 bytes allocated
==69691==
==69691== LEAK SUMMARY:
==69691== definitely lost: 16 bytes in 1 blocks
==69691== indirectly lost: 0 bytes in 0 blocks
==69691== possibly lost: 13,090 bytes in 117 blocks
==69691== still reachable: 26,259 bytes in 311 blocks
==69691== suppressed: 0 bytes in 0 blocks
==69691== Rerun with --leak-check=full to see details of leaked memory
==69691==
==69691== For counts of detected and suppressed errors, rerun with: -v
==69691== Use --track-origins=yes to see where uninitialised values come from
==69691== ERROR SUMMARY: 5 errors from 2 contexts (suppressed: 0 from 0)
我在OS X优胜美地。 Valgrind是通过 BREW
安装使用此命令 $冲泡安装的valgrind --HEAD
。
因此,没有人知道什么是这里的问题?我如何修复Valgrind的错误?
So, does anyone know what's the issue here? How do I fix the valgrind errors?
推荐答案
如果您通过Valgrind的运行程序正是你在你的问题张贴之一,它显然不具备任何内存泄漏。事实上,你甚至不使用malloc /释放自己!
If the programme you are running through Valgrind is exactly the one you posted in your question, it clearly doesn't have any memory leaks. In fact, you don't even use malloc/free yourself!
在我看来像Valgrind的在OS X(仅!)检测这些都是虚假的错误/误报,类似<一个href=\"http://stackoverflow.com/questions/8657337/what-do-the-sup$p$pssed-leaks-mean-in-valgrind\">what前一段时间发生在我自己。
It looks to me like these are spurious errors / false positives that Valgrind detects on OS X (only!), similar to what happened to myself some time ago.
如果你有机会到不同的操作系统,例如一台Linux机器,尝试分析该系统上使用Valgrind的程序。
If you have access to a different operating system, e.g. a Linux machine, try to analyze the programme using Valgrind on that system.
编辑:我没有尝试过这个我自己,因为我没有访问到Mac的权利,但你应该尝试什么
中号Oehm建议:尝试使用苏pressions文件作为本提到其他SO质疑。
I haven't tried this myself, since I don't have access to a Mac right now, but you should try what M Oehm suggested: try to use a supressions file as mentioned in this other SO question.
这篇关于Valgrind的报告错误一个非常简单的C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!