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

Community
  • 1
  • 1
Beginner C
  • 79
  • 3
  • 12
  • realloc returns a void *, so needs a cast. But more importantly, what are you trying to do? Change something inside a single Parameter? – doctorlove Oct 01 '13 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|, ...)` – Beginner C Oct 01 '13 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?) – ryyker Oct 01 '13 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. – Beginner C Oct 01 '13 at 15:42
  • It looks like you are compiling it with a C++ compiler. These error messages are typical for C++. – n. m. could be an AI Oct 01 '13 at 15:51
  • @n.m. I tried compiling same code with Tiny C, and in the line `p[i] = realloc(p[i],sizeof(Parameters) * typetotal)` the error message is: "cannot cast 'struct parameters' to 'void *' – Beginner C Oct 01 '13 at 16:12
  • That's a C error. You need to fix it but you need to start using a C compiler first. – n. m. could be an AI Oct 01 '13 at 16:15
  • @n.m okay, I will start using Tiny C. But do you know how to solve my problem? – Beginner C Oct 01 '13 at 16:16
  • Update your question with C error messages, and replace your description of the Parameters struct with a declaration copied from the actual code. – n. m. could be an AI Oct 01 '13 at 16:26
  • Your `Parameters` struct makes no sense at all, and is not in fact legal C. It's hard to tell what are you trying to achieve. – n. m. could be an AI Oct 01 '13 at 16:49
  • @n.m.Fixed. Does it make sense now? – Beginner C Oct 01 '13 at 16:54
  • Not in the slightest. I've no clue how did you manage to avoid getting tons of error messages on all the `hero` fields of your struct. Not even speaking about what you actually *want* to do with them. Sorry. – n. m. could be an AI Oct 01 '13 at 18:44

2 Answers2

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);
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • The configuration of VS has already been made before, but I do not know why it is giving me c++ errors. – Beginner C Oct 01 '13 at 16:37
  • What do you mean by "old_typenum" ? – Beginner C Oct 01 '13 at 16:38
  • @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. – chux - Reinstate Monica Oct 01 '13 at 17:19
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

Community
  • 1
  • 1
Pankrates
  • 3,074
  • 1
  • 22
  • 28
  • why do you need to put `int i` when your code is not using it? – Beginner C Oct 01 '13 at 16:42
  • 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? – Beginner C Oct 01 '13 at 16:46
  • You keep writing `p->type` but in your definition of `Parameters` there is no member called `type`. – Pankrates Oct 01 '13 at 17:06
  • What I tried to refer `p->type` as was the `hero **type[][]`. How do I do this then? – Beginner C Oct 01 '13 at 17:14
  • 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 – Pankrates Oct 01 '13 at 17:37
  • Thanks! Can you leave your email address here so that I could email to you my code – Beginner C Oct 01 '13 at 17:56