Guest User

Untitled

a guest
Nov 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. char **tab;
  2.  
  3. // allocation
  4. tab = malloc(5 * sizeof(char*));
  5. for(int j=0 ; j<5 ; j++)
  6. {
  7. tab[j] = malloc(4 * sizeof(char));
  8. }
  9.  
  10. // FILL TAB FIRST WAY
  11. /*
  12. for(int j=0 ; j<5 ; j++)
  13. {
  14. for(int i=0 ; i<4 ; i++)
  15. {
  16. tab[j][i] = '@';
  17. }
  18. }
  19. */
  20.  
  21. // FILL TAB SECOND WAY
  22. for(int j=0 ; j<5 ; j++)
  23. {
  24. tab[j] = "@@@@";
  25. }
  26.  
  27. //free
  28. for(int j=0 ; j<5 ; j++)
  29. {
  30. free(tab[j]);
  31. }
  32.  
  33. free(tab);
  34.  
  35. HEAP SUMMARY:
  36. ==447== in use at exit: 20 bytes in 5 blocks
  37. ==447== total heap usage: 6 allocs, 6 frees, 60 bytes allocated
  38. ==447==
  39. ==447== Searching for pointers to 5 not-freed blocks
  40. ==447== Checked 64,648 bytes
  41. ==447==
  42. ==447== LEAK SUMMARY:
  43. ==447== definitely lost: 20 bytes in 5 blocks
  44. ==447== indirectly lost: 0 bytes in 0 blocks
  45. ==447== possibly lost: 0 bytes in 0 blocks
  46. ==447== still reachable: 0 bytes in 0 blocks
  47. ==447== suppressed: 0 bytes in 0 blocks
  48. ==447== Rerun with --leak-check=full to see details of leaked memory
  49. ==447==
  50. ==447== ERROR SUMMARY: 5 errors from 1 contexts (suppressed: 0 from 0)
  51. ==447==
  52. ==447== 5 errors in context 1 of 1:
  53. ==447== Invalid free() / delete / delete[] / realloc()
  54. ==447== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-
  55. amd64-linux.so)
  56. ==447== by 0x400607: main (test_malloc.c:35)
Add Comment
Please, Sign In to add comment