Consider the following program: 1 #include ostdlib.h» #include 3 #include s- typ
ID: 3709760 • Letter: C
Question
Consider the following program: 1 #include ostdlib.h» #include 3 #include s- typedef struct 6 7 char nome; 8 double cost; 9 Iten; 10 11-Item* createIte char none, double cost) 13 Item item malloc(sizeof(Item)); 14 item->cost cost 15 16 item->nome malloc(strlenCname) ): 17 strcpyCitem-name, name); 18 19 return item; 2e 21 22e int main(void) 23 t 24 Item table createItemC Table, 199.99); 25 printf("s x.2fn, table->name, table->cost) 26 27 28 return : The program seems to work, printing out "Table 199.99. But valgrind flags a number of problems: % valgrind -1eak-check-yes ..show-leak-kinds-all ./Leak -17858 Memcheck, a memory error detector 17858 Copyright (C) 2802-2013, and GNU GPL'd, by Julian Sewa rd et al. 17058 using valgrind-3.10.e and LibvEX; rerun with -h for co pyright info 17858s Command: ./Leak -17858- Table 199,99 17858 -17058 HEAP SUMARY 17958 in use at exiti 22 bytes in 2 blocks -17058s total heap usage: 2 allocs, a frees, 22 bytes alloca ted 17958 1 of 2 17658at x4C290F0: malloc (n u/libea/valgrind/vgprel 17058 6 bytes in 1 blocks are indirectly lost in loss record oad mencheck-amd64-linux, so) 17058a by 8x4006521 createlten (Leas.c:16) 17858 by 0x400600: main (Leak.124) 17058Explanation / Answer
Valgrind find Lot os issues because:
1) We are allocating memory using malloc but we never deallocated hence valgrind gave us that data
2) There is lot of memory leak because we never delallocated the memory
Thanks, let me know if you need more information. PLEASE UPVOTE if helpful