0

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 (...);

4

2 回答 2

1

OP is coding in C, but using a using a C++ compiler.

Code in C++

// C 
// p = realloc(p,sizeof(Parameters *) * typenum);
// C++
p = (Parameters *) realloc(p,sizeof(Parameters *) * typenum);

OR

VS2012: set properties for each C file to use C compiler

How to compile C in visual studio 2010?


OP code has a memory leak when scaling down the pointer array table. The pointers in the table that are about to be loss due to realloc() need to be freed first.

for (i=old_typenum; i<typenum; i++) free(p[i]);
p = realloc(p,sizeof(Parameters *) * typenum);
于 2013-10-01T16:23:46.200 回答
1

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

于 2013-10-01T16:25:47.083 回答