2

I'm trying to bolster my understanding of C having never really delved into it properly and have taken to following the 'Learn C The Hard Way' tutorials online as a starting point. I'm a reasonably confident programmer but I thought starting right from the beginning would be the best way to ensure my full understanding of the concepts having come from mostly using higher level languages like Python and C#.

Exercise 6 on variable types gives the following sample to run (condensed to relevant lines only):

#include <stdio.h>

int main(int argc, char *argv[])
{
    float power = 2.345f;

    printf("You have %f levels of power.\n", power);

    return 0;
}

While compiling and running this there's no issue, but the tutorial series encourages the use of Valgrind to help debugging. Upon running Valgrind, I notice the following:

HEAP SUMMARY:
in use at exit: 26,032 bytes in 187 blocks
total heap usage: 271 allocs, 84 frees, 32,264 bytes allocated

148 (80 direct, 68 indirect) bytes in 1 blocks are definitely lost in loss record 43 of 66
at 0x100007D81: malloc (vg_replace_malloc.c:303)
by 0x1001C78D6: __Balloc_D2A (in /usr/lib/system/libsystem_c.dylib)
by 0x1001C821F: __d2b_D2A (in /usr/lib/system/libsystem_c.dylib)
by 0x1001C4877: __dtoa (in /usr/lib/system/libsystem_c.dylib)
by 0x1001ED3E6: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
by 0x1002166C8: __v2printf (in /usr/lib/system/libsystem_c.dylib)
by 0x1001EC389: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
by 0x1001EA223: printf (in /usr/lib/system/libsystem_c.dylib)
by 0x100000F58: main (ex6.c:7)

LEAK SUMMARY:
definitely lost: 80 bytes in 1 blocks
indirectly lost: 68 bytes in 2 blocks
  possibly lost: 0 bytes in 0 blocks
still reachable: 0 bytes in 0 blocks
     suppressed: 25,884 bytes in 184 blocks

While it isn't noted as an error or a warning, I'm curious as to what this means? I'm familiar with memory leaks as being memory that is used and then not freed up and I get the feeling that this is something to do with the precision of the float but I'd like to truly understand what is going on here and what the significance of this leak is? Sadly the tutorials don't mention anything on it.

alk
  • 69,737
  • 10
  • 105
  • 255
Hexodus
  • 369
  • 6
  • 18
  • 1
    271 allocs are A LOT. Are you linking this code to some library that allocates data on load for example using something like `__attribute__((constructor))`? I don't think `printf()` needs to allocate anything at all. Please post the commands you are using to compile the program. Also, please note that on OS X [valgrind](http://www.valgrind.org) often has wierd behavior primarily for the lack of suppression files I think. – Iharob Al Asimi Jul 21 '16 at 13:12
  • Also it would be interesting to know using which compiler (version) you are using and on which OS you are observing this on. Is this clang and OSX? – alk Jul 21 '16 at 13:16
  • 2
    It has nothing to do with the float. See [here](http://stackoverflow.com/questions/12452541/valgrind-reports-leaked-memory-on-os-x-10-8-1?rq=1) – StoryTeller - Unslander Monica Jul 21 '16 at 13:20
  • @iharob @alk I'm compiling with `cc -Wall -g ex6.c -o ex6` on `Apple LLVM version 7.3.0 (clang-703.0.31)` if that has any bearing on this? I'm using OS X 10.11 for which Valgrind does report only 'initial support' but I glossed over that thinking it would probably be fine for these simple tutorials. Are there any good alternatives out there that might be a little more actively maintained? – Hexodus Jul 21 '16 at 13:51
  • @StoryTeller Thanks for the link, if it is a compatibility issue with Valgrind that's fine. I just wanted to make sure I wasn't doing something stupid from the get go. – Hexodus Jul 21 '16 at 13:56

0 Answers0