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?
If you dynamically allocate (malloc)
struct aas inyou
mallocspace for a pointer tostruct b(assuming that’s what is instruct a) but you don’tmallocspace forstruct b. That means later you’ll have to dobefore you try and use
struct b.If you don’t store a pointer to
struct bbut ratherstruct bdirectly then you’ll get the automatic allocation when you definestruct a.