6

I am dynamically allocating a struct which has a different struct as a member:

struct a {
   // other members
   struct b;
}

struct b basically holds a pointer to another struct b, so think of struct b as a linked list.

If I dynamically allocate struct a, then that would also make a new struct b within it. However, what is the difference between doing that or having struct a hold a pointer to struct b, and dynamically allocate struct b within struct a? What is the difference in implementation?

3 Answers 3

10

First, let's get some real definitions in place to make this concrete.

struct b {
    int x;
};

struct a_with_b {
    struct b b;
}

struct a_with_b_ptr {
    struct b *bp;
}

When you encapsulate a struct, you just need to allocate the outer struct (and since the inner struct is not a pointer, you use . to reference members of the innert struct):

struct a_with_b *a1 = malloc(sizeof(struct a_with_b));
a1->b.x = 3;

But when you encapsulate a pointer, you have to allocate each independently and use -> when referencing members of the inner struct:

struct a_with_b_ptr *a2 = malloc(sizeof(struct a_with_b_ptr));
a1->b = malloc(sizeof(struct b));
a1->b->x = 3;
9

If you dynamically allocate (malloc) struct a as in

struct a *temp = (struct a *)malloc(sizeof(struct a));

you malloc space for a pointer to struct b (assuming that's what is in struct a) but you don't malloc space for struct b. That means later you'll have to do

temp->b = (struct b *)malloc(sizeof(struct b));

before you try and use struct b.

If you don't store a pointer to struct b but rather struct b directly then you'll get the automatic allocation when you define struct a.

3

The difference is really equivalent to any other situation where you're comparing "automatic" and "dynamic" allocation.1

In terms of a guide as when you should use a pointer member, I would say that you should avoid it unless there's a good reason not to, due to the programmer overhead in dealing with manual memory management (and the bugs that it inevitably leads to).

An example of a good reason would be if you needed your struct a to refer to an existing struct b.


1. "automatic" is the term used in the C standard for this sort of allocation, because the memory is cleaned up automatically.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.