RicardasSim

cleanup();

Aug 10th, 2020 (edited)
1,615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. //-std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7.  
  8. #define TEST_A_MAX_SIZE 1000000
  9.  
  10. typedef struct test_a
  11. {
  12.     uint32_t *variables;
  13.     uint32_t test_count;
  14. } s_test_a;
  15.  
  16. /*
  17. C99 6.7.8
  18. 10.
  19. If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.
  20. If an object that has static storage duration is not initialized explicitly, then:
  21. -- if it has pointer type, it is initialized to a null pointer;
  22. -- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  23. -- if it is an aggregate, every member is initialized (recursively) according to these rules;
  24. -- if it is a union, the first named member is initialized (recursively) according to these rules.
  25. */
  26.  
  27. s_test_a s_1;
  28. s_test_a *s_2;
  29.  
  30. void cleanup()
  31. {
  32.     if ( s_1.variables != NULL )
  33.     {
  34.         free (s_1.variables);
  35.     }
  36.  
  37.     if( s_2 != NULL )
  38.     {
  39.         if ( s_2->variables != NULL )
  40.         {
  41.             free ( s_2->variables );
  42.         }
  43.  
  44.         free ( s_2 );
  45.     }
  46.  
  47. }
  48.  
  49. int main()
  50. {
  51.  
  52.     printf("(1) %p\n", (void*) &s_1);
  53.     printf("(2) %p\n", (void*) s_1.variables);
  54.     printf("(3) %p\n", (void*) s_2);
  55.  
  56.     s_1.variables = malloc( TEST_A_MAX_SIZE * sizeof (uint32_t) );
  57.  
  58.     if ( !s_1.variables )
  59.     {
  60.         printf("Error: cannot allocate memory (1).\n");
  61.         cleanup();
  62.         return 1;
  63.     }
  64.  
  65.     s_2 = malloc( sizeof *s_2 );
  66.  
  67.     if ( !s_2 )
  68.     {
  69.         printf("Error: cannot allocate memory (2).\n");
  70.         cleanup();
  71.         return 1;
  72.     }
  73.  
  74.     s_2->variables = malloc( TEST_A_MAX_SIZE * sizeof (uint32_t) );
  75.  
  76.     if ( !s_2->variables )
  77.     {
  78.         printf("Error: cannot allocate memory (3).\n");
  79.         cleanup();
  80.         return 1;
  81.     }
  82.  
  83.     printf("(4) %p\n", (void*) &s_1);
  84.     printf("(5) %p\n", (void*) s_1.variables);
  85.     printf("(6) %p\n", (void*) s_2);
  86.  
  87.     s_1.test_count = TEST_A_MAX_SIZE;
  88.     s_2->test_count = 10;
  89.  
  90.  
  91.     printf( "%d\n", s_1.test_count );
  92.     printf( "%d\n", s_2->test_count );
  93.  
  94.     s_1.variables[TEST_A_MAX_SIZE-1] = 1;
  95.     s_2->variables[10] = 2;
  96.  
  97.     printf( "%d\n", s_1.variables[TEST_A_MAX_SIZE-1] );
  98.     printf( "%d\n", s_2->variables[10] );
  99.  
  100.     cleanup();
  101.  
  102.     return 0;
  103. }
  104.  
  105. /*
  106. output:
  107.  
  108. (1) 0x559a0e727020
  109. (2) (nil)
  110. (3) (nil)
  111. (4) 0x559a0e727020
  112. (5) 0x7f8d767df010
  113. (6) 0x559a0f0196b0
  114. 1000000
  115. 10
  116. 1
  117. 2
  118.  
  119. valgrind:
  120.  
  121. HEAP SUMMARY:
  122. in use at exit: 0 bytes in 0 blocks
  123. total heap usage: 4 allocs, 4 frees, 8,001,040 bytes allocated
  124.  
  125. All heap blocks were freed -- no leaks are possible
  126.  
  127. For lists of detected and suppressed errors, rerun with: -s
  128. ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  129.  
  130. */
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment