RicardasSim icon

attempt, realloc up zero

RicardasSim | PRO | 08/08/21 07:33:44 PM UTC | 0 ⭐ | 1681 👁️ | Never ⏰ | []
C |

3.12 KB

|

None

|

0 👍

/

0 👎

//-Wall -Wextra -Wpedantic -Wshadow
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
 
#define NUM_OF_SEGM 4
 
typedef struct{
uint8_t a;
uint32_t b;
uint32_t *p;
}TestStruct;
 
void CleanupSt( TestStruct *s, uint32_t n )
{
    for ( uint32_t i = 0; i < n; ++i )
    {
        free( s[i].p );
    }
}
 
void* ReallocUpZero( void *p, size_t old_size_in_bytes, size_t new_size_in_bytes )
{
    char *p_tmp;
 
    if ( new_size_in_bytes == old_size_in_bytes )
    {   
        printf("Error: the new size is equal to the old one.\n");
        return NULL;
    }
    else if( new_size_in_bytes < old_size_in_bytes )
    {
        printf("Error: the new size smaller than the old one.\n");
        return NULL;
    }
 
    p_tmp = realloc( p, new_size_in_bytes );
 
    if ( !p_tmp )
    {
        printf("Error: (rz2) cannot reallocate memory.\n");
        return false;
    }
 
    memset( p_tmp + old_size_in_bytes, 0, new_size_in_bytes - old_size_in_bytes );
 
    return p_tmp;
}
 
int main()
{
 
    TestStruct *tst;
    uint32_t num_of_segm;
 
    num_of_segm = NUM_OF_SEGM;
 
    tst = calloc( num_of_segm, sizeof *tst );
 
    if ( !tst )
    {
        printf("Error: cannot allocate memory.\n");
        return 1;
    }
 
    printf("----1----\n");
 
    for ( uint32_t i = 0; i < num_of_segm; ++i )
    {
        printf("(%d) %d %d %p\n", i, tst[i].a, tst[i].b, (void*) tst[i].p );
    }
 
    for ( uint32_t i = 0; i < num_of_segm; ++i )
    {
        tst[i].a = 1;
        tst[i].b = 2;
        tst[i].p = malloc(10);
        if (! tst[i].p)
        {
            printf("Error: (a1) cannot allocate memory.\n");
            
            CleanupSt( tst, num_of_segm );
 
            free( tst );
 
            return 1;
        }
    }
 
    printf("----2----\n");
 
    for ( uint32_t i = 0; i < num_of_segm; ++i )
    {
        printf("(%d) %d %d %p\n", i, tst[i].a, tst[i].b, (void*) tst[i].p );
    }
 
    uint32_t new_num_of_segm = num_of_segm + NUM_OF_SEGM;
 
    void *p_tmp = ReallocUpZero( tst, num_of_segm * sizeof *tst, new_num_of_segm * sizeof *tst );
 
    if( !p_tmp )
    {
        printf("Error: (ReallocUpZero) cannot reallocate memory.\n");
        CleanupSt( tst, num_of_segm );
        free( tst );
        return 1;        
    }
 
    num_of_segm = new_num_of_segm;
    tst = p_tmp;
 
    printf("----3----\n");
 
    for ( uint32_t i = 0; i < num_of_segm; ++i )
    {
        printf("(%d) %d %d %p\n", i, tst[i].a, tst[i].b, (void*) tst[i].p );
    }
 
    CleanupSt( tst, num_of_segm );
 
    free( tst );
 
    return 0;
}
 
/*
output:
 
----1----
(0) 0 0 (nil)
(1) 0 0 (nil)
(2) 0 0 (nil)
(3) 0 0 (nil)
----2----
(0) 1 2 0x4a81500
(1) 1 2 0x4a81550
(2) 1 2 0x4a815a0
(3) 1 2 0x4a815f0
----3----
(0) 1 2 0x4a81500
(1) 1 2 0x4a81550
(2) 1 2 0x4a815a0
(3) 1 2 0x4a815f0
(4) 0 0 (nil)
(5) 0 0 (nil)
(6) 0 0 (nil)
(7) 0 0 (nil)
 
valgrind: 
in use at exit: 0 bytes in 0 blocks
total heap usage: 7 allocs, 7 frees, 1,256 bytes allocated
All heap blocks were freed -- no leaks are possible
 
*/
 

Comments