0

I have the following code.:

struct O1 {
    int a;
    int b;
    int c;
};

struct O2 {
    int f;
    int g;
    struct O1 *array;      //pointer to an array of structs O1
};

struct O2 *list = malloc(25 * sizeof(struct O1));        //array of 25 structs O2

struct O1 *innerlist = malloc(sizeof(struct O1));    //array of structs O1 contained inside each struct O2

innerlist[0].a = 1;
innerlist[0].b = 2;
innerlist[0].c = 3;

(*list)[0].f = 1;
(*list)[0].g = 1;
(*list)[0].array = &innerlist[0];    //filling the parameters via some loop for example

So my question is, did I allocate the arrays correctly?

For the inner structure contained inside each struct O2, I only wanted an array with just one element (one struct O1) that I could then expand further via realloc for example but I tried storing more struct O1s in struct O2 and it works so there must have been more memory allocated somewhere even though I did not multiply the sizeof by anything when creating the array of struct O1.

I also have a question, how do I free the arrays?

I guess I must first free the inner array first but I am having trouble here.

I have created a function for freeing the memory that takes the array of struct O2 as a parameter.

But when I type free(list->obj); it seems to be freeing the obj of the first struct O2 in the list and free(list[n]->obj) does not seem to be working.

4
  • Numeric identifiers like struct 01 will not compile. Did you intend capital-O struct O1? There are some missing ; too after the struct definitions. Is this your code? Commented Dec 3, 2015 at 17:47
  • This code does not compile. Please show us some compilable code that exhibits the issue. Commented Dec 3, 2015 at 17:52
  • Also (*list)[0] is syntactically equivalent to *((*list)+0). This is wrong I think. You said your example compiles. Can you post that example please? Commented Dec 3, 2015 at 17:54
  • It's difficult to answer when you provide that has too many compiler errors. Commented Dec 3, 2015 at 17:56

1 Answer 1

2

1 Create an array of O2:

struct O2 *list = malloc(25 * sizeof(struct O2)); 

2 Allocate space for 1 O1 in each O2

for (int i = 0; i < 25; i++) {
    list[i].array = malloc(sizeof(struct O1));
}

3 Free the mem

for (int i = 0; i < 25; i++) {
    free(list[i].array);
}
free(list);
Sign up to request clarification or add additional context in comments.

9 Comments

This looks good. Will that allocate an array of structs 01 though but with just one element? I mean, can I expand it later and add more structs 01 to the array?
Yes. In the original post you said you wanted just one element. You can allocate more - malloc(sizeof(struct O1) * 100); - or realloc later.
Yes thanks a lot. How about filling the elements of struct 01 though? Lets say inside a function where the argument is the array of structs 02. I have tried (*list)[i].obj->id = 1; for example but this does not work.
You would treat it like a regular array: list[x].array[y].a = 1;
Something is wrong with my allocation as the program crashes. My function takes a parameter struct 02 **ptr which is a pointer to a pointer to the array. I then allocate the main array like this *ptr = malloc(25 * sizeof(struct O2)); Then run the for loop like this for (int i = 0; i < 25; i++) { ptr[i]->array = malloc(sizeof(struct O1)); And try to fill the parameters of the structures like this: (*ptr)[n].f = 1; (*ptr)[n].array[0].a = 1;
|

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.