By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,787 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,787 IT Pros & Developers. It's quick & easy.

How to find the size of structure?

P: 96
hello guys, Can u tell me how to find the size of structure.
Please explain by giving some example.
Thank you
Jun 12 '10 #1
Share this Question
Share on Google+
7 Replies

alexis4
100+
P: 113
Add the size of your structure's members and you will have the structure's size!

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.   int a;
  4.   float b;
  5. } MY_STRUCT;

The size of MY_STRUCT would be sizeof(int) + sizeof(float).
So you can use the sizeof() function to acquire this information during runtime.

Expand|Select|Wrap|Line Numbers
  1. unsigned char MY_STRUCTSize = sizeof(MY_STRUCT);

And if you want to verify that the structure is the sum of its members:

Expand|Select|Wrap|Line Numbers
  1. MY_STRUCT myStruct;
  2. unsigned char myStructSize = sizeof(myStruct);
  3. unsigned char myStructSizea = sizeof(myStruct.a);
  4. unsigned char myStructSizeb = sizeof(myStruct.b);

Note that I am not giving you fixed sizes because they depend on the platform and compiler. For my embedded enviroment's compiler, variables sizes are platform independent. So if I compile the above code I will get sizeof(myStruct.a) = 2, sizeof(myStruct.b) = 4 and sizeof(myStruct) = 6.
Jun 12 '10 #2

Banfa
Expert Mod 5K+
P: 8,997
Add the size of your structure's members and you will have the structure's size!

The size of MY_STRUCT would be sizeof(int) + sizeof(float).
No generally this is not true.

Expand|Select|Wrap|Line Numbers
  1. MY_STRUCT myStruct;
  2. unsigned char myStructSize = sizeof(myStruct);
  3. unsigned char myStructSizea = sizeof(myStruct.a);
  4. unsigned char myStructSizeb = sizeof(myStruct.b);
  5.  
If you do this you need to be very careful because on the majority of platforms

myStructSize != myStructSizea + myStructSizeb

because the compiler puts padding bytes into the structure to maintain correct member alignment for efficient access.

sizeof(myStruct) will give you the size of the structure including any padding bytes, for example should you want to use malloc to allocate memory for it.


The are some platforms, typically 8bit ones where the compiler does not include any padding bytes on which the size of the structure is the sum of the size of its members.
Jun 12 '10 #3

P: 96
@Banfa
can u give the example in which padding is used so can know what is the size of structure.
Jun 12 '10 #4

Banfa
Expert Mod 5K+
P: 8,997
The example already given could have padding. Like I said most structures could. Typically padding occurs when you have a structure with several differently sized objects in it for example

Expand|Select|Wrap|Line Numbers
  1. struct example1
  2. {
  3.   char a;
  4.   short b;
  5.   float c;
  6. };
  7.  
  8. struct example2
  9. {
  10.   float c;
  11.   short b;
  12.   char a;
  13. };
How much padding a structure has is entirely platform dependent.

In this case sizeof(example1) will always return the size of the structure on the current platform, including padding bytes.
Jun 13 '10 #5

P: 96
@Banfa
if we declare two integer varibles then what is the size of structure.
like this
Expand|Select|Wrap|Line Numbers
  1. struct st1
  2. {
  3. int a;
  4. int b;
  5. }
  6.  
in my opinion the ans is 4
but the actual ans is 2
i donot know what is the reason behind this
Jun 14 '10 #6

Dheeraj Joshi
Expert 100+
P: 1,123
When i run this code in my Linux machine (gcc version 3.4.6 20060404). The output is
Expand|Select|Wrap|Line Numbers
  1. 8
  2.  
This is as expected(since int is taking 4 bytes.)

If your machine is taking 2 bytes to represent an int then the size of the structure will be 4.

Regards
Dheeraj Joshi
Jun 14 '10 #7

Banfa
Expert Mod 5K+
P: 8,997
@AnagJohari
in my opinion the ans is 4
but the actual ans is 2
i donot know what is the reason behind this
neither do we/I unless you show us the code you used to get the "actual answer".
Jun 14 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.