464,787 Members | 1,369 Online
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
7 Replies

 100+ P: 113 Add the size of your structure's members and you will have the structure's size! Expand|Select|Wrap|Line Numbers typedef struct {   int a;   float b; } 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 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 MY_STRUCT myStruct; unsigned char myStructSize = sizeof(myStruct); unsigned char myStructSizea = sizeof(myStruct.a); 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

 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 MY_STRUCT myStruct; unsigned char myStructSize = sizeof(myStruct); unsigned char myStructSizea = sizeof(myStruct.a); unsigned char myStructSizeb = sizeof(myStruct.b);   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

 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 struct example1 {   char a;   short b;   float c; };   struct example2 {   float c;   short b;   char a; }; 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 struct st1 { int a; int b; }   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

 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 8   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

 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