Created
September 18, 2012 17:21
-
-
Save reagent/3744427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
struct Person { | |
char *name; | |
}; | |
struct Person *create_person_stack(char *name) | |
{ | |
struct Person person; | |
person.name = name; | |
struct Person *person_ptr = &person; | |
return person_ptr; | |
} | |
struct Person *create_person_heap(char *name) | |
{ | |
struct Person *person_ptr = malloc(sizeof(struct Person)); | |
person_ptr->name = name; | |
return person_ptr; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
struct Person *person_1 = create_person_stack("Patrick"); | |
struct Person *person_2 = create_person_heap("Patrick"); | |
printf("Person 1's name is %s.\n", person_1->name); | |
printf("Person 2's name is %s.\n", person_2->name); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
==22497== Memcheck, a memory error detector | |
==22497== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. | |
==22497== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info | |
==22497== Command: ./stack_allocation | |
==22497== | |
==22497== Use of uninitialised value of size 8 | |
==22497== at 0x1000134F6: strlen (mc_replace_strmem.c:282) | |
==22497== by 0x100034B1B: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Use of uninitialised value of size 8 | |
==22497== at 0x100013503: strlen (mc_replace_strmem.c:282) | |
==22497== by 0x100034B1B: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Use of uninitialised value of size 8 | |
==22497== at 0x10001361C: memchr (mc_replace_strmem.c:558) | |
==22497== by 0x100038306: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Use of uninitialised value of size 8 | |
==22497== at 0x100013634: memchr (mc_replace_strmem.c:558) | |
==22497== by 0x100038306: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Conditional jump or move depends on uninitialised value(s) | |
==22497== at 0x100014A6F: memcpy (mc_replace_strmem.c:77) | |
==22497== by 0x100038394: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Conditional jump or move depends on uninitialised value(s) | |
==22497== at 0x100014A85: memcpy (mc_replace_strmem.c:635) | |
==22497== by 0x100038394: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Conditional jump or move depends on uninitialised value(s) | |
==22497== at 0x100014A93: memcpy (mc_replace_strmem.c:635) | |
==22497== by 0x100038394: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Conditional jump or move depends on uninitialised value(s) | |
==22497== at 0x100014AE0: memcpy (mc_replace_strmem.c:635) | |
==22497== by 0x100038394: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Conditional jump or move depends on uninitialised value(s) | |
==22497== at 0x100014AF5: memcpy (mc_replace_strmem.c:635) | |
==22497== by 0x100038394: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Conditional jump or move depends on uninitialised value(s) | |
==22497== at 0x100014BD9: memcpy (mc_replace_strmem.c:635) | |
==22497== by 0x100038394: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Use of uninitialised value of size 8 | |
==22497== at 0x100014BF0: memcpy (mc_replace_strmem.c:635) | |
==22497== by 0x100038394: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
==22497== Use of uninitialised value of size 8 | |
==22497== at 0x100014C01: memcpy (mc_replace_strmem.c:635) | |
==22497== by 0x100038394: __sfvwrite (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100037CFB: __vfprintf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100075DCA: vfprintf_l (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x1000A3482: printf (in /usr/lib/libSystem.B.dylib) | |
==22497== by 0x100000E67: main (stack_allocation.c:32) | |
==22497== | |
Person 1's name is Patrick. | |
Person 2's name is Patrick. | |
==22497== | |
==22497== HEAP SUMMARY: | |
==22497== in use at exit: 4,192 bytes in 3 blocks | |
==22497== total heap usage: 3 allocs, 0 frees, 4,192 bytes allocated | |
==22497== | |
==22497== LEAK SUMMARY: | |
==22497== definitely lost: 8 bytes in 1 blocks | |
==22497== indirectly lost: 0 bytes in 0 blocks | |
==22497== possibly lost: 0 bytes in 0 blocks | |
==22497== still reachable: 4,184 bytes in 2 blocks | |
==22497== suppressed: 0 bytes in 0 blocks | |
==22497== Rerun with --leak-check=full to see details of leaked memory | |
==22497== | |
==22497== For counts of detected and suppressed errors, rerun with: -v | |
==22497== Use --track-origins=yes to see where uninitialised values come from | |
==22497== ERROR SUMMARY: 28 errors from 12 contexts (suppressed: 0 from 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment