Calculating the size of a complex struct (C)


I need to calculate the size of a complex struct which includes an array of pointers to another struct. Some of the structs in the array might be allocated, some might not be allocated.

So in the sample below, let's say 5 of the 10 pointers in my_child_struct might be allocated. I know how many elements of the array are allocated but how do I calculate the total size of MyParentStruct so I can memcpy to another MyParentStruct instance?

struct MyParentStruct
	MyChildStruct *my_child_struct[10];

struct MyChildStruct
	char name[10];
	char description[20];

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi php-newbie,

since the 'my_child_struct' is an array of pointers the size of 'MyParentStruct' should always be 10*sizeof(MyChildStruct*), no matter if you allocated data for any entry of 'my_child_struct'. Due to the same reason a simple 'memcpy' won't really copy your data, it would copy the pointers but keep them pointing to the original data.

So, to create a copy you need to copy the allocated entries of 'my_child_struct' one by one, i.e. like in this pseude code:

> for ( int n = 0; n < 10; n++ )
> {
>  if ( /*is n'th entry of source's 'my_child_struct' allocated*/ )
>  {
>   /* allocate n'th entry of destination's 'my_child_struct' and use memcpy with 'sizeof( MyChildStruct )' to copy its data */
>  }
> }

Hope that helps,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sizeof(struct MyParentStruct)
evilrixSenior Software Engineer (Avast)Commented:
You can't calculate it because the compiler is free to add padding for the sake or Word Alignment, therefore the answer ozo have given is the only safe way you can do this.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

you can't find out the size of an array when you only have a pointer to that array (at least not with normal c++). so if you not only need the size of the struct but also the sizes used for dynamic allocation you need to store the number of elements with each pointer and manually add-up the sizes.

Deepu AbrahamR & D Engineering ManagerCommented:

I agree with Sara,

sizeof(struct MyParentStruct)
I think it is the size of the pointer multiplied by 10 (say if pointer size is 4 bytes in your compiler then the size would be 40)

char* p=NULL;

char *p1[10];

Also you need to understand whether you are doing a deep copy or shallow copy. You didn't indicate. A simple mempcy() of the parent struct will be a shallow copy, which is in short what Zoppo said. If you don't understand the difference, look it up before continuing, because your problem may be simpler than you think if you need a shallow copy.
trinitrotolueneDirector - Software EngineeringCommented:
">>>calculate the total size of MyParentStruct so I can memcpy to another MyParentStruct instance""

As already mentioned the question of a deep and shallow copy comes up. If you need to do a shallow copy then all that happens is that the 10 pointers to MyChildStruct  will get copied.

So effectively you would have 10 child structs but 2 or more parents pointing to them.

But if you need a deep copy done then you would need to create a copy of each of the 10 child structs one for each parent struct that you create from the original parent.

And you really don't need to bother about how many child structs you have. Blindly doing a memcpy is also not the way to go. Just ensure that your 10 child pointers are initialized to NULL and then when you do a copy allocate memory for a child only if the original child is not NULL.
as the questioner told in the original post "i know how many elements of the array are allocated"  i think it is sure that a deep copy is wanted.

the wrong assumption was in the following when the author meant one single memcpy could do a deep copy.

php-newbie, memcpy only can copy memory parts which were contiguous and were allocated with one call to malloc. so in your case you have one allocation for MyParentStruct and some for MyChildStruct and a deep copy would need to repeat all those allocations.

for the MyParentStruct you should add a init function which would initialize the array with NULL pointers like trintrotoluene has suggested. then for doing a deep copy you would do a loop like the one zoppo has shown in the first comment and would implement the '*is n'th entry of source's 'my_child_struct' allocated' like.

if (ps1->my_child_struct[i] != NULL) {
ps2->my_child_struct[i] = (MyChildStruct*)malloc(sizeof(MyChildStruct));
memcpy(ps2->my_child_struct[i], ps1->my_child_struct[i], sizeof(MyChildStruct);}

Open in new window

the ps1, ps2 were assumed to be valid pointers to MyParentStruct both properly initialized.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.