RicardasSim

attempt, realloc up zero

Aug 8th, 2021
1,715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. //-Wall -Wextra -Wpedantic -Wshadow
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #include <string.h>
  7.  
  8. #define NUM_OF_SEGM 4
  9.  
  10. typedef struct{
  11. uint8_t a;
  12. uint32_t b;
  13. uint32_t *p;
  14. }TestStruct;
  15.  
  16. void CleanupSt( TestStruct *s, uint32_t n )
  17. {
  18.     for ( uint32_t i = 0; i < n; ++i )
  19.     {
  20.         free( s[i].p );
  21.     }
  22. }
  23.  
  24. void* ReallocUpZero( void *p, size_t old_size_in_bytes, size_t new_size_in_bytes )
  25. {
  26.     char *p_tmp;
  27.  
  28.     if ( new_size_in_bytes == old_size_in_bytes )
  29.     {  
  30.         printf("Error: the new size is equal to the old one.\n");
  31.         return NULL;
  32.     }
  33.     else if( new_size_in_bytes < old_size_in_bytes )
  34.     {
  35.         printf("Error: the new size smaller than the old one.\n");
  36.         return NULL;
  37.     }
  38.  
  39.     p_tmp = realloc( p, new_size_in_bytes );
  40.  
  41.     if ( !p_tmp )
  42.     {
  43.         printf("Error: (rz2) cannot reallocate memory.\n");
  44.         return false;
  45.     }
  46.  
  47.     memset( p_tmp + old_size_in_bytes, 0, new_size_in_bytes - old_size_in_bytes );
  48.  
  49.     return p_tmp;
  50. }
  51.  
  52. int main()
  53. {
  54.  
  55.     TestStruct *tst;
  56.     uint32_t num_of_segm;
  57.  
  58.     num_of_segm = NUM_OF_SEGM;
  59.  
  60.     tst = calloc( num_of_segm, sizeof *tst );
  61.  
  62.     if ( !tst )
  63.     {
  64.         printf("Error: cannot allocate memory.\n");
  65.         return 1;
  66.     }
  67.  
  68.     printf("----1----\n");
  69.  
  70.     for ( uint32_t i = 0; i < num_of_segm; ++i )
  71.     {
  72.         printf("(%d) %d %d %p\n", i, tst[i].a, tst[i].b, (void*) tst[i].p );
  73.     }
  74.  
  75.     for ( uint32_t i = 0; i < num_of_segm; ++i )
  76.     {
  77.         tst[i].a = 1;
  78.         tst[i].b = 2;
  79.         tst[i].p = malloc(10);
  80.         if (! tst[i].p)
  81.         {
  82.             printf("Error: (a1) cannot allocate memory.\n");
  83.            
  84.             CleanupSt( tst, num_of_segm );
  85.  
  86.             free( tst );
  87.  
  88.             return 1;
  89.         }
  90.     }
  91.  
  92.     printf("----2----\n");
  93.  
  94.     for ( uint32_t i = 0; i < num_of_segm; ++i )
  95.     {
  96.         printf("(%d) %d %d %p\n", i, tst[i].a, tst[i].b, (void*) tst[i].p );
  97.     }
  98.  
  99.     uint32_t new_num_of_segm = num_of_segm + NUM_OF_SEGM;
  100.  
  101.     void *p_tmp = ReallocUpZero( tst, num_of_segm * sizeof *tst, new_num_of_segm * sizeof *tst );
  102.  
  103.     if( !p_tmp )
  104.     {
  105.         printf("Error: (ReallocUpZero) cannot reallocate memory.\n");
  106.         CleanupSt( tst, num_of_segm );
  107.         free( tst );
  108.         return 1;        
  109.     }
  110.  
  111.     num_of_segm = new_num_of_segm;
  112.     tst = p_tmp;
  113.  
  114.     printf("----3----\n");
  115.  
  116.     for ( uint32_t i = 0; i < num_of_segm; ++i )
  117.     {
  118.         printf("(%d) %d %d %p\n", i, tst[i].a, tst[i].b, (void*) tst[i].p );
  119.     }
  120.  
  121.     CleanupSt( tst, num_of_segm );
  122.  
  123.     free( tst );
  124.  
  125.     return 0;
  126. }
  127.  
  128. /*
  129. output:
  130.  
  131. ----1----
  132. (0) 0 0 (nil)
  133. (1) 0 0 (nil)
  134. (2) 0 0 (nil)
  135. (3) 0 0 (nil)
  136. ----2----
  137. (0) 1 2 0x4a81500
  138. (1) 1 2 0x4a81550
  139. (2) 1 2 0x4a815a0
  140. (3) 1 2 0x4a815f0
  141. ----3----
  142. (0) 1 2 0x4a81500
  143. (1) 1 2 0x4a81550
  144. (2) 1 2 0x4a815a0
  145. (3) 1 2 0x4a815f0
  146. (4) 0 0 (nil)
  147. (5) 0 0 (nil)
  148. (6) 0 0 (nil)
  149. (7) 0 0 (nil)
  150.  
  151. valgrind:
  152. in use at exit: 0 bytes in 0 blocks
  153. total heap usage: 7 allocs, 7 frees, 1,256 bytes allocated
  154. All heap blocks were freed -- no leaks are possible
  155.  
  156. */
  157.  
Advertisement
Add Comment
Please, Sign In to add comment