0

I am having a structure that includes a set of other structures:

 typedef struct{
    int cell_Type;      //user association: 0--> Macro 1--> Femto
    int cell_ID;        //cell ID that the user is associated with
}association;

 //User struct
 typedef struct{
    coord c;                //coordinates
    association id;             //association parameters
    capacity cap;               //capacity parameters   
 }usu;

 //Struct with a vector of users
 typedef struct{
     usu user[NUM_USERS+NUM_HS]; //each user is defined by the association (id), etc...
 }vectusers;

void main(int argc,char *argv[])
{
       //.....

       //memory allocation for the structures
       users_ptr=calloc(1,sizeof(vectusers)); //users pointer

       //.....
}

So far so good, up to this point that there has been the need for an array that I will not know its size until the program is almost finished.

As such, the structure will be:

 typedef struct{
    int cell_Type;      //user association: 0--> Macro 1--> Femto
    int cell_ID;        //cell ID that the user is associated with
    int assigned_RBs;   //counter of how many resources have been assigned
    int *RB_number;     //array with size according to assigned_RBs counter
}association;

My problem is with the malloc() and realloc(). Actually I think I am reserving correctly memory with the malloc:

users_ptr->user[u].id.RB_number = malloc(1*sizeof(int));

but when it comes to realloc(), I do not change the size of the array:

users_ptr->user[index_max_W].id.assigned_RBs++;

size = users_ptr->user[index_max_W].id.assigned_RBs;

users_ptr->user[index_max_W].id.RB_number = realloc((users_ptr->user[index_max_W].id.RB_number),size);

where index_max_w is the index of the user with a max value of a statistic. Both size and index_max_W have been declared as int.

Could someone please help me with the realoc in this case?

4
  • 1
    It is not clear what is wrong. What do you mean by 'but when it comes to realloc(), I do not change the size of the array:' Commented Feb 3, 2014 at 9:24
  • In the begining the array has only one entry, which will be the [0]. After some execution I want it to have more entries, in example 3, according to some statistics. This will be change according to the assigne_RBs Commented Feb 3, 2014 at 9:39
  • If this code is supposed to compile and run on a hosted environment (such as Windows, Linux, phones etc), then main must return int. Commented Feb 3, 2014 at 10:40
  • Up to this point I did not have any problems. The only difference now is this array that I cannot figure out how to create it and change its size. As I said, what I would like to have is: In the begining have an array: users_ptr->user[index_max_W].id.RB_number[0] (with only one entry) and later on to change its size (e.g. to 3), as such having users_ptr->user[index_max_W].id.RB_number[0], users_ptr->user[index_max_W].id.RB_number[1], users_ptr->user[index_max_W].id.RB_number[2] Commented Feb 3, 2014 at 10:58

1 Answer 1

1

The size argument to realloc() is in bytes, just like the argument to malloc(). Your code seems to omit tihs scaling for the realloc(), which will lead to (drastic) under-allocation and thus undefined behavior if you access the memory.

It should be something like:

const size_t new_size = users_ptr->user[index_max_W].id.assigned_RBs;

users_ptr->user[index_max_W].id.RB_number = realloc(users_ptr->user[index_max_W].id.RB_number,
new_size * sizeof *users_ptr->user[index_max_W].id.RB_number);

It's a bit unwieldy due to the nested name of the target pointer. We can simplify that by using a suitable temporary pointer variable to remove the repeated complicated access:

usu *user = &users_ptr->user[index_max_W];
user->id.assigned_RBs++;
user->id.RB_number = realloc(user->id.RB_number,
                             user->id.assigned_RBs * sizeof *user->id.RB_number);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response. I am trying with this way, but still it doesn't seem to work.
I changed the realloc() as in the first suggestion, however the variable size I have it as int. This way it works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.