RicardasSim

attempt, realloc zero

Aug 6th, 2021 (edited)
1,414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.85 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. bool ReallocZero( 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.         /*
  31.         printf("Warning: the new size is equal to the old one.\n");
  32.         return true; */
  33.        
  34.         printf("Error: the new size is equal to the old one.\n");
  35.         return false;
  36.     }
  37.     else if( new_size_in_bytes < old_size_in_bytes )
  38.     {
  39.         /* also tried to reduce the size
  40.  
  41.         printf("Warning: the new size smaller than the old one.\n");
  42.  
  43.         p_tmp = realloc( *p, new_size_in_bytes );
  44.  
  45.         if ( !p_tmp )
  46.         {
  47.             printf("Error: (rz1) cannot reallocate memory.\n");
  48.             return false;
  49.         }
  50.  
  51.         *p = p_tmp;
  52.  
  53.         return true;
  54.         */
  55.  
  56.         printf("Error: the new size smaller than the old one.\n");
  57.  
  58.         return false;
  59.     }
  60.  
  61.     p_tmp = realloc( *p, new_size_in_bytes );
  62.  
  63.     if ( !p_tmp )
  64.     {
  65.         printf("Error: (rz2) cannot reallocate memory.\n");
  66.         return false;
  67.     }
  68.  
  69.     memset( p_tmp + old_size_in_bytes, 0, new_size_in_bytes - old_size_in_bytes );
  70.  
  71.     *p = p_tmp;
  72.  
  73.     return true;
  74. }
  75.  
  76. int main()
  77. {
  78.  
  79.     TestStruct *tst;
  80.     uint32_t num_of_segm;
  81.  
  82.     num_of_segm = NUM_OF_SEGM;
  83.  
  84.     tst = calloc( num_of_segm, sizeof *tst );
  85.  
  86.     if ( !tst )
  87.     {
  88.         printf("Error: cannot allocate memory.\n");
  89.         return 1;
  90.     }
  91.  
  92.     printf("----1----\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.     for ( uint32_t i = 0; i < num_of_segm; ++i )
  100.     {
  101.         tst[i].a = 1;
  102.         tst[i].b = 2;
  103.         tst[i].p = malloc(10);
  104.         if (! tst[i].p)
  105.         {
  106.             printf("Error: (a1) cannot allocate memory.\n");
  107.            
  108.             CleanupSt( tst, num_of_segm );
  109.  
  110.             free( tst );
  111.  
  112.             return 1;
  113.         }
  114.     }
  115.  
  116.     printf("----2----\n");
  117.  
  118.     for ( uint32_t i = 0; i < num_of_segm; ++i )
  119.     {
  120.         printf("(%d) %d %d %p\n", i, tst[i].a, tst[i].b, (void*) tst[i].p );
  121.     }
  122.  
  123.     uint32_t new_num_of_segm = num_of_segm + NUM_OF_SEGM;
  124.  
  125.     /* also tried to reduce the size
  126.     uint32_t new_num_of_segm = num_of_segm - 2;
  127.  
  128.     if ( new_num_of_segm < num_of_segm )
  129.     {
  130.         for ( uint32_t i = new_num_of_segm; i < num_of_segm; ++i )
  131.         {
  132.             free( tst[i].p );
  133.         }
  134.     }
  135.     */
  136.  
  137.     if( !ReallocZero( (void*)&tst, num_of_segm * sizeof *tst, new_num_of_segm * sizeof *tst ) )
  138.     {
  139.         printf("Error: (ReallocZero) cannot reallocate memory.\n");
  140.         CleanupSt( tst, num_of_segm );
  141.         free( tst );
  142.         return 1;        
  143.     }
  144.  
  145.     num_of_segm = new_num_of_segm;
  146.  
  147.     printf("----3----\n");
  148.  
  149.     for ( uint32_t i = 0; i < num_of_segm; ++i )
  150.     {
  151.         printf("(%d) %d %d %p\n", i, tst[i].a, tst[i].b, (void*) tst[i].p );
  152.     }
  153.  
  154.     CleanupSt( tst, num_of_segm );
  155.  
  156.     free( tst );
  157.  
  158.     return 0;
  159. }
  160.  
  161. /*
  162. output:
  163.  
  164. ----1----
  165. (0) 0 0 (nil)
  166. (1) 0 0 (nil)
  167. (2) 0 0 (nil)
  168. (3) 0 0 (nil)
  169. ----2----
  170. (0) 1 2 0x4a81500
  171. (1) 1 2 0x4a81550
  172. (2) 1 2 0x4a815a0
  173. (3) 1 2 0x4a815f0
  174. ----3----
  175. (0) 1 2 0x4a81500
  176. (1) 1 2 0x4a81550
  177. (2) 1 2 0x4a815a0
  178. (3) 1 2 0x4a815f0
  179. (4) 0 0 (nil)
  180. (5) 0 0 (nil)
  181. (6) 0 0 (nil)
  182. (7) 0 0 (nil)
  183.  
  184. valgrind:
  185. HEAP SUMMARY:
  186. in use at exit: 0 bytes in 0 blocks
  187. total heap usage: 7 allocs, 7 frees, 1,256 bytes allocated
  188. All heap blocks were freed -- no leaks are possible
  189.  
  190. */
  191.  
Advertisement
Add Comment
Please, Sign In to add comment