Search code examples
carraysfunctionrealloc

Creating function out of realloc() function


I would like to create a function that will reallocate 2D array of typedef struct

typedef struct hero_data{
    char name[254];
    char title[254];
    int encoding;
    int startstr;
    double incstr;
    int startdex;
    double incdex;
    int startintel;
    double incintel;
    int basemindmg,basemaxdmg;
    double bat;
    double basearmor;
    struct hero_data *next;
    struct hero_data *Class;
}hero;

typedef struct parameters{ 
    int toughtotal;
    int nimbletotal;
    int smarttotal;
    int skeptictotal;
    int mystictotal;
    int cursedtotal;
    int brutetotal;
    int shreddertotal;
    int vanillatotal;
    int typetotal;
    int typenum;
    hero **smart[];
    hero **nimble[];
    hero **tough[]; 
    hero **type[][];
    hero **skeptic[][];
    hero **mystic[][];
    hero **cursed[][];
    hero **brute[][];
    hero **shredder[][];
    hero **vanilla[][];
}Parameters;

void reallocation(Parameters *p, int typenum,int typetotal)
{
    int i;

    p = realloc(p,sizeof(Parameters *) * typenum);
    for ( i = 0; i < typenum; i++)
    {
        p[i] = realloc(p[i],sizeof(Parameters) * typetotal);
    }
}

The function above shall be called like: void reallocation(p->type,p->typenum,p->typetotal);

So, by substituting the parameters of the function correctly, I expect the function to look like:

void reallocation(Parameters *p, int typenum,int typetotal)
{
    int i;

    p->type = realloc(p->type,sizeof(Parameters *) * p->typenum);
    for ( i = 0; i < p->typenum; i++)
    {
        p->type[i] = realloc(p->type[i],sizeof(Parameters) * p->typetotal);
    }
}

The typedef struct named Parameters contains int typenum, int typetotal, and the 2D arrays that shall be initialized through realloc().

When I try to compile, I am getting an error in Tiny C (Windows): *The file is in C.

  1. Error: cannot cast 'struct parameters' to 'void *'

    (This apeears in the 'p[i] = realloc(p[i],sizeof(Parameters) * typetotal')

Can anyone help me re-write this function so that I will be able to realloc the 2D arrays within the Parameter *p?


I tried changing void reallocation(Parameters *p, ...) into void reallocation(Parameters *p[], ...) and the Error # 2 becomes the same message as Error #1 and it appears in the = of p[i] = realloc (...);


Solution

  • A large problem with your code is that you are assigning inequal types to each other, and you are also not checking the result of realloc. If this call were to fail, you will leak the memory allocated initially.

    Assuming that your struct looks like

    typedef struct {
        int typenum;
        int typetotal;
    } Parameters;
    
    Parameters *p;
    
    p = malloc(10 * sizeof(*p));
    if (p == NULL)
        printf("Allocatation of memory failed!\n");
    

    To properly reallocate to say 20, you could do something like this

    reallocate_p(&p, 20);
    

    Where the function is defined as

    void reallocate_p(Parameters **p, int new_size)
    {
        Parameters *temp;
    
        temp = realloc(*p, sizeof(*temp) * new_size);
        if (temp==NULL) {
            printf("Reallocatation of memory failed!\n");
            // Handle error        
        }
    
        *p = temp;
    
        return;
    }
    

    Also note that we don't cast the return value of malloc() and realloc(). As to why, see this reference