我的.c文件代码:
#include <stdio.h>
/* i had made the coding wrong on purpose. */
int main(){
int g = 10;
int h;
printf("I am %d age.\n");
printf("I am %dtall.\n", h);
return 0;
}
我运行了这段代码:gcc -Wall -g -o ex4 ex4.c
在我的终端中,这是我得到的输出:
ex4.c:8:17: warning: more '%' conversions than data arguments [-Wformat]
printf("I am %d age.\n");
~^
ex4.c:5:7: warning: unused variable 'g' [-Wunused-variable]
int g = 10;
^
ex4.c:9:28: warning: variable 'h' is uninitialized when used here
[-Wuninitialized]
printf("I am %dtall.\n", h);
^
ex4.c:6:8: note: initialize the variable 'h' to silence this warning
int h;
^
= 0
3 warnings generated.
我继续尝试和valgrind一起检查内存泄漏:
valgrind ./ex4
这是我终端上的输出:
iPatrickMac:trying_valgrind patrick$ valgrind ./ex4
==10836== Memcheck, a memory error detector
==10836== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10836== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==10836== Command: ./ex4
==10836==
==10836== Syscall param msg->desc.port.name points to uninitialised byte(s)
==10836== at 0x1003A834A: mach_msg_trap (in /usr/lib/system/libsystem_kernel.dylib)
==10836== by 0x1003A7796: mach_msg (in /usr/lib/system/libsystem_kernel.dylib)
==10836== by 0x1003A1485: task_set_special_port (in /usr/lib/system/libsystem_kernel.dylib)
==10836== by 0x10053D10E: _os_trace_create_debug_control_port (in /usr/lib/system/libsystem_trace.dylib)
==10836== by 0x10053D458: _libtrace_init (in /usr/lib/system/libsystem_trace.dylib)
==10836== by 0x1000A69DF: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==10836== by 0x100018A1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==10836== by 0x100018C1D: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==10836== by 0x1000144A9: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==10836== by 0x100014440: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==10836== by 0x100013523: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==10836== by 0x1000135B8: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==10836== Address 0x10488bdcc is on thread 1's stack
==10836== in frame #2, created by task_set_special_port (???:)
==10836==
I am 76077616 age.
==10836== Conditional jump or move depends on uninitialised value(s)
==10836== at 0x1002A64D7: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002CE422: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A433D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A2247: printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x100000F67: main (ex4.c:9)
==10836==
==10836== Conditional jump or move depends on uninitialised value(s)
==10836== at 0x1002A8E87: __ultoa (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002CE422: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A433D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A2247: printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x100000F67: main (ex4.c:9)
==10836==
==10836== Conditional jump or move depends on uninitialised value(s)
==10836== at 0x10009A142: _platform_memchr$VARIANT$Haswell (in /usr/local/Cellar/valgrind/3.13.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==10836== by 0x10029E4D1: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A8898: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002CE422: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A433D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A2247: printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x100000F67: main (ex4.c:9)
==10836==
==10836== Syscall param write(buf) points to uninitialised byte(s)
==10836== at 0x1003B047E: write$NOCANCEL (in /usr/lib/system/libsystem_kernel.dylib)
==10836== by 0x1002A3200: _swrite (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x10029BD26: __sflush (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x10029E584: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A8952: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002CE422: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A433D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A2247: printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x100000F67: main (ex4.c:9)
==10836== Address 0x100b57b85 is 5 bytes inside a block of size 4,096 alloc'd
==10836== at 0x100098616: malloc (in /usr/local/Cellar/valgrind/3.13.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==10836== by 0x10029EFD8: __smakebuf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002B3B1D: __swsetup (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002CE1BE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A433D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x1002A2247: printf (in /usr/lib/system/libsystem_c.dylib)
==10836== by 0x100000F53: main (ex4.c:8)
==10836==
I am 1tall.
==10836==
==10836== HEAP SUMMARY:
==10836== in use at exit: 22,136 bytes in 160 blocks
==10836== total heap usage: 176 allocs, 16 frees, 28,280 bytes allocated
==10836==
==10836== LEAK SUMMARY:
==10836== definitely lost: 0 bytes in 0 blocks
==10836== indirectly lost: 0 bytes in 0 blocks
==10836== possibly lost: 72 bytes in 3 blocks
==10836== still reachable: 200 bytes in 6 blocks
==10836== suppressed: 21,864 bytes in 151 blocks
==10836== Rerun with --leak-check=full to see details of leaked memory
==10836==
==10836== For counts of detected and suppressed errors, rerun with: -v
==10836== Use --track-origins=yes to see where uninitialised values come from
==10836== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 4 from 4)
我很困惑为什么我的valgrind不显示错误行并显示其他路径。
我的编译有什么问题,或者macOS sierra上的valgrind有什么问题吗?帮帮我们):
这是什么:
==10836== Syscall param msg->desc.port.name points to uninitialised byte(s)
==10836== at 0x1003A834A: mach_msg_trap (in /usr/lib/system/libsystem_kernel.dylib)
==10836== by 0x1003A7796: mach_msg (in /usr/lib/system/libsystem_kernel.dylib)
==10836== by 0x1003A1485: task_set_special_port (in /usr/lib/system/libsystem_kernel.dylib)
==10836== by 0x10053D10E: _os_trace_create_debug_control_port (in /usr/lib/system/libsystem_trace.dylib)
==10836== by 0x10053D458: _libtrace_init (in /usr/lib/system/libsystem_trace.dylib)
==10836== by 0x1000A69DF: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==10836== by 0x100018A1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==10836== by 0x100018C1D: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==10836== by 0x1000144A9: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==10836== by 0x100014440: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==10836== by 0x100013523: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==10836== by 0x1000135B8: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==10836== Address 0x10488bdcc is on thread 1's stack
==10836== in frame #2, created by task_set_special_port (???:)
显示错误行的valgrind示例:
==5190== Use of uninitialised value of size 4
==5190== at 0x4078B2B: _itoa_word (_itoa.c:195)
==5190== by 0x407CE55: vfprintf (vfprintf.c:1619)
==5190== by 0x40831DE: printf (printf.c:35)
==5190== by 0x4052112: (below main) (libc-start.c:226)
==5190==
==5190== Conditional jump or move depends on uninitialised value(s)
==5190== at 0x4078B33: _itoa_word (_itoa.c:195)
==5190== by 0x407CE55: vfprintf (vfprintf.c:1619)
==5190== by 0x40831DE: printf (printf.c:35)
==5190== by 0x4052112: (below main) (libc-start.c:226)
==5190==
==5190== Conditional jump or move depends on uninitialised value(s)
==5190== at 0x407CC10: vfprintf (vfprintf.c:1619)
==5190== by 0x40831DE: printf (printf.c:35)
==5190== by 0x4052112: (below main) (libc-start.c:226)
==5190==
==5190== Conditional jump or move depends on uninitialised value(s)
==5190== at 0x407C742: vfprintf (vfprintf.c:1619)
==5190== by 0x40831DE: printf (printf.c:35)
==5190== by 0x4052112: (below main) (libc-start.c:226)
如何在我的macOS sierra上允许我的valgrind相应地显示错误行,而不显示其他奇怪的路径,我不知道它们是什么。
答案 0 :(得分:0)
C11标准,章节§7.21.6.1
[..]如果格式的参数不足,则行为未定义。 [..]
因此,您最好不要忽略此警告,因为此调用的结果行为是未定义。