C: Effects Of Accessing Uninitialized Variables Using printf()
https://github.com/pereiradaniel/beginning_c/blob/master/ch2/init.c
// init.c // Daniele Grech Pereira, 02 November 2022 // Demonstration of variables before and after initialization. #include <stdio.h> #define LENGTH(a) sizeof(a) / sizeof(a[0]) void display(char* string, int array[], int size); int main(int argc, char* argv[]) { int num[10] = {1,2,3,4,5,6,7,8,9,10}; // Simultaneously declare and initialize a variable. int num2[10]= {1,2,3}; // First three are specified, the rest set to 0. int num3[10]= {0}; // Easy way to initialize all array values with 0. int arr[10]; // Declare an array of ints without initializing. // Printing the uninitialized array int arr[] will display garbage and generate warnings // from valgrind. display("Printing int num[] which has been initialized with data:", num, LENGTH(num)); display("Printing int num2[] which has been initialized with data:", num2, LENGTH(num2)); display("Printing int num3[] which has been initialized with data:", num3, LENGTH(num3)); display("Printing int arr[] which has not been initialized with data:", arr, LENGTH(arr)); // Initialize int arr[] for (int i=0; i<LENGTH(arr);++i) arr[i] = LENGTH(arr) - i; display("Printing int arr[] after initialization:", arr, LENGTH(arr)); return 0; } void display(char* string, int array[], int size) { printf("%s\n\t", string); for (int i = 0; i < size; ++i) { printf("%d", array[i]); i == size-1 ? printf(".\n") : printf(", "); } printf("\n"); }
https://github.com/pereiradaniel/beginning_c/blob/master/ch2/init.txt
==309== Memcheck, a memory error detector ==309== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==309== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==309== Command: ./init ==309== Printing int num[] which has been initialized with data: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Printing int num2[] which has been initialized with data: 1, 2, 3, 0, 0, 0, 0, 0, 0, 0. Printing int num3[] which has been initialized with data: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0. Printing int arr[] which has not been initialized with data: ==309== Conditional jump or move depends on uninitialised value(s) ==309== at 0x48D3AD8: __vfprintf_internal (vfprintf-internal.c:1687) ==309== by 0x48BDEBE: printf (printf.c:33) ==309== by 0x1093C0: display (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== by 0x1092F1: main (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== ==309== Use of uninitialised value of size 8 ==309== at 0x48B781B: _itoa_word (_itoa.c:179) ==309== by 0x48D36F4: __vfprintf_internal (vfprintf-internal.c:1687) ==309== by 0x48BDEBE: printf (printf.c:33) ==309== by 0x1093C0: display (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== by 0x1092F1: main (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== ==309== Conditional jump or move depends on uninitialised value(s) ==309== at 0x48B782D: _itoa_word (_itoa.c:179) ==309== by 0x48D36F4: __vfprintf_internal (vfprintf-internal.c:1687) ==309== by 0x48BDEBE: printf (printf.c:33) ==309== by 0x1093C0: display (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== by 0x1092F1: main (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== ==309== Conditional jump or move depends on uninitialised value(s) ==309== at 0x48D43A8: __vfprintf_internal (vfprintf-internal.c:1687) ==309== by 0x48BDEBE: printf (printf.c:33) ==309== by 0x1093C0: display (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== by 0x1092F1: main (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== ==309== Conditional jump or move depends on uninitialised value(s) ==309== at 0x48D386E: __vfprintf_internal (vfprintf-internal.c:1687) ==309== by 0x48BDEBE: printf (printf.c:33) ==309== by 0x1093C0: display (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== by 0x1092F1: main (in /mnt/c/Users/perei/source/repos/beginning_c/ch2/init) ==309== 77897672, 0, 1086480, 0, 0, 0, 1085600, 0, -16776768, 31. Printing int arr[] after initialization: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1. ==309== ==309== HEAP SUMMARY: ==309== in use at exit: 0 bytes in 0 blocks ==309== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated ==309== ==309== All heap blocks were freed -- no leaks are possible ==309== ==309== Use --track-origins=yes to see where uninitialised values come from ==309== For lists of detected and suppressed errors, rerun with: -s ==309== ERROR SUMMARY: 104 errors from 5 contexts (suppressed: 0 from 0)