tôi đang học ngôn ngữ C từ Learn C The Hard Way. Tôi đang ở trên exercise 6 và trong khi tôi có thể làm cho nó hoạt động, valgrind repots rất nhiều lỗi.Valgrind báo cáo lỗi cho một chương trình C rất đơn giản
Đây là tước xuống chương trình tối thiểu từ một tập tin 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;
}
Nội dung Makefile
chỉ CFLAGS=-Wall -g
là.
Tôi biên dịch chương trình với $ make ex6
(không có cảnh báo hoặc lỗi trình biên dịch). Việc thực thi với $ ./ex6
sẽ tạo ra kết quả mong đợi.
Khi tôi chạy chương trình với $ valgrind ./ex6
Tôi gặp lỗi mà tôi không thể giải quyết được. Đây là đầu ra đầy đủ:
==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)
Tôi đang ở trên OS X yosemite. Valgrind được cài đặt thông qua brew
với lệnh này $ brew install valgrind --HEAD
.
Vì vậy, có ai biết vấn đề ở đây là gì không? Làm cách nào để khắc phục lỗi valgrind?
Bạn đang liên kết với một số thư viện bên ngoài? –
Trên Linux của tôi, ví dụ của bạn không báo cáo bất kỳ lỗi nào. Trong thanh bên Liên kết có liên quan, có [một gợi ý sử dụng tệp nén] (http://stackoverflow.com/questions/5226691/valgrind-mac-os-mem-leak?rq=1). –
@IngoLeonhardt: Các đối số biến thể của loại 'float' được thăng thành' double', do đó không phải là vấn đề. (Và nguyên mẫu chính xác là trong ' ', được bao gồm.) –