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

12
  • realloc returns a void *, so needs a cast. But more importantly, what are you trying to do? Change something inside a single Parameter? Commented Oct 1, 2013 at 15:29
  • @doctorlove My program needs to initialize around nine 2D arrays that are within typedef struct named Parameter. I just wanted to reallocate each one of them by putting the names of the arrays in void reallocation (Parameters *p |<- Here|, ...) Commented Oct 1, 2013 at 15:33
  • If you had a way to initialize, allocate, then free existing structs, then would it work for you to use the same functions that accomplised those tasks, within a new function "reallocation", to accomplish the re-allocation? i.e. just include the free and allocate functions, with new parameters? Also, it is important that your argument list follow a prescribed set, or are you free to choose what argument you want to use? (that is, is this an assignment with prescribed requirements?) Commented Oct 1, 2013 at 15:39
  • @ryyker I plan to use the reallocation function to initialize the arrays and re-allocate their memory since int typenum and int typetotal increases when conditions are met within the code. And I realized I needed to repear the re-allocating function over and over again. Commented Oct 1, 2013 at 15:42
  • It looks like you are compiling it with a C++ compiler. These error messages are typical for C++. Commented Oct 1, 2013 at 15:51

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

why do you need to put int i when your code is not using it?
I added for ( i = 0; i < typenum; i ++) {temp = realloc(*p, sizeof(*temp) * typetotal);} below temp = realloc(*p, ...);. I tried using that void function by calling: reallocate_p(p->type,p->typenum,p->typetotal); and I am getting Warning: assignement from incompatible pointer type. Any help?
You keep writing p->type but in your definition of Parameters there is no member called type.
What I tried to refer p->type as was the hero **type[][]. How do I do this then?
Ok, at this point it starts to be clear to me that there is a lot going wrong with your code. More so than can be easily covered in this question. I could take maybe take a look at it over email if you want
|
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);

3 Comments

The configuration of VS has already been made before, but I do not know why it is giving me c++ errors.
What do you mean by "old_typenum" ?
@Beginner C To properly reallocate the pointer array, the re-allocation routine needs to know the old size of the array. With realloc(), this information is hidden from the user. Since you have a more complex type (an array of pointers that in turn point to data) and you may re-allocate to a smaller array of pointers, the excess pointers need to have the data they point to freed. Ah - I see you have all ready selected a solution. Good luck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.