Search code examples
cstructdynamic-memory-allocationreallocfunction-definition

How do I write my own realloc for table of structure?


I am writing a bigger project, and I have a problem with reallocating the array. Firstly I tried using the function realloc, but it leaves memory leaks and errors (the project must be compatible with Valgrind). Then I tried writing my own realloc, but it does not work, and I have no idea why. Please help (I am reallocating a table of structures).

typedef struct {
    char w[2]; //word
    int v; //value
    int d; //direction
    char b; //binary
}node_t;

int realoc(node_t *huff, int length, int expand_size){
    node_t *temp = malloc((length+expand_size) * sizeof(node_t));
    for(int i=0; i<length ;i++){
        for(int j=0;j<2;j++)
            temp[i].w[j] = huff[i].w[j];
        temp[i].v = huff[i].v;
        temp[i].d = huff[i].d;
        temp[i].b = huff[i].b;
        huff=temp;
    }
    return length+expand_size
}

Solution

  • The previously dynamically allocated memory pointed to by the pointer node_t *huff was not freed.

    Also do not name your functions similar to C standard functions. This only confuses readers of the code.

    You could just write using the standard C function realloc for example

    node_t *temp = realloc( huff, (length+expand_size) * sizeof(node_t));
    
    if ( temp != NULL ) huff = temp;
    

    In this case there is no need to copy data because the function realloc itself copies them in the new memory extent.

    And the function has to accept the pointer buff by reference indirectly through a pointer to pointer. Otherwise it changes its local parameter buff. And in this case the function should return an integer that reports whether the memory was reallocated successfully. For example

    int realloc_node( node_t **huff, int length, int expand_size )
    {
        node_t *temp = realloc( *huff, (length+expand_size) * sizeof(node_t));
    
        if ( temp != NULL ) *huff = temp;
    
        return temp != NULL;
    }
    

    Here is a demonstration program.

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
        char w[2]; //word
        int v; //value
        int d; //direction
        char b; //binary
    }node_t;
    
    int realloc_node( node_t **huff, int length, int expand_size )
    {
        node_t *temp = realloc( *huff, ( length + expand_size ) * sizeof( node_t ) );
    
        if (temp != NULL) *huff = temp;
    
        return temp != NULL;
    }
    
    int main( void )
    {
        int length = 2;
    
        node_t *huff = malloc( 2 * sizeof( node_t ) );
    
        for (int i = 0; i < length; i++)
        {
            huff[i].w[0] = 'A' + i;
            huff[i].w[1] = 'B' + i;
            huff[i].v = i;
            huff[i].d = i;
            huff[i].b = 'C';
        }
    
        int expand_size = 2;
    
        if (realloc_node( &huff, length, expand_size ))
        {
            for (int i = length; i < length + expand_size; i++)
            {
                huff[i].w[0] = 'A' + i;
                huff[i].w[1] = 'B' + i;
                huff[i].v = i;
                huff[i].d = i;
                huff[i].b = 'C' + i;
            }
    
            length += expand_size;
    
            for (int i = 0; i < length; i++)
            {
                printf( "%.2s, %d, %d, %c\n", huff[i].w, huff[i].v, huff[i].d, huff[i].b );
            }
        }
    
        free( huff );
    }
    

    The program output is

    AB, 0, 0, C
    BC, 1, 1, C
    CD, 2, 2, E
    DE, 3, 3, F