I have a problem with the extra credit for exercise 16 from Learn C The Hard Way ( http://c.learncodethehardway.org/book/ex16.html ), I'm trying to convert structs, so they don't use malloc. I came up with this code (I'm not sure if I have to paste the source code or the link enough):
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char *name;
int age;
int height;
int weight;
};
void Person_print(struct Person who)
{
printf("Name: %s\n", who.name);
printf("\tAge: %d\n", who.age);
printf("\tHeight: %d\n", who.height);
printf("\tWeight: %d\n", who.weight);
}
int main(int argc, char *argv[])
{
struct Person joe = {"Joe Alex", 32, 64, 140};
struct Person frank = {"Frank Blank", 20, 72, 180};
printf("Joe is at memory location %p:\n", &joe);
Person_print(joe);
printf("Frank is at memory location %p:\n", &frank);
Person_print(frank);
joe.age += 20;
joe.height -= 2;
joe.weight += 40;
Person_print(joe);
frank.age += 20;
frank.weight += 20;
Person_print(frank);
return 0;
}
The code compiles without any errors or warnings and works as expected. If I ran it Valgrind, I get the following output:
==8556== Memcheck, a memory error detector
==8556== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==8556== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==8556== Command: ./ex16
==8556==
Joe is at memory location 0x104802928:
Name: Joe Alex
==8556== Conditional jump or move depends on uninitialised value(s)
==8556== at 0x1003FCC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==8556== by 0x1001F0BB6: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==8556== by 0x1001FB005: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==8556== by 0x1002209CE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==8556== by 0x100220CA0: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==8556== by 0x1001F6B91: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==8556== by 0x1001F49F7: printf (in /usr/lib/system/libsystem_c.dylib)
==8556== by 0x100000D58: Person_print (ex16.c:16)
==8556== by 0x100000E34: main (ex16.c:28)
==8556==
Age: 32
Height: 64
Weight: 140
Frank is at memory location 0x104802910:
Name: Frank Blank
Age: 20
Height: 72
Weight: 180
Name: Joe Alex
Age: 52
Height: 62
Weight: 180
Name: Frank Blank
Age: 40
Height: 72
Weight: 200
==8556==
==8556== HEAP SUMMARY:
==8556== in use at exit: 38,691 bytes in 425 blocks
==8556== total heap usage: 504 allocs, 79 frees, 44,803 bytes allocated
==8556==
==8556== LEAK SUMMARY:
==8556== definitely lost: 16 bytes in 1 blocks
==8556== indirectly lost: 0 bytes in 0 blocks
==8556== possibly lost: 13,130 bytes in 120 blocks
==8556== still reachable: 25,545 bytes in 304 blocks
==8556== suppressed: 0 bytes in 0 blocks
==8556== Rerun with
==8556==
==8556== For counts of detected and suppressed errors, rerun with: -v
==8556== Use
==8556== ERROR SUMMARY: 12 errors from 1 contexts (suppressed: 0 from 0)
I can not understand the conclusion of Valgrind here, I will be very grateful for any explanation. I tried to comment on these lines in the Print_person function:
/* printf("\tAge: %d\n", who.age); */
/* printf("\tHeight: %d\n", who.height); */
/* printf("\tWeight: %d\n", who.weight); */
Valgrind , , .