473,320 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to find the size of structure?

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 41664
alexis4
113 100+
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
9,065 Expert Mod 8TB
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
@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
9,065 Expert Mod 8TB
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
@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
1,123 Expert 1GB
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
9,065 Expert Mod 8TB
@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

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

Similar topics

3
by: Manish Jain | last post by:
Platform: C# (Windows Application)/XP/Framework 1.1 ----------------------------------------------------------- Hi I am doing some performance checks, I am using a hashtable that I suspect is...
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
3
by: Henrootje | last post by:
I have a database in which regurlarily tables are being created or deleted. This causes a lot of bloating. I set the property that causes the db to compress and repair at close but my users...
1
by: bss | last post by:
X is a data structure. we have to find out size of X without creating any instance of X and also sizeof() operator should not be used.
2
by: Robin9876 | last post by:
How can you find out the size of tables in a database without having to export each of the tables?
1
by: pldeepthireddy | last post by:
Hi, I have a windows application in which i have a form and there is some functionality.On the click of a button ,i need to get a message saying "The size of the form is "XX" Kb ".HOw i do i get the...
2
by: Chetanhl | last post by:
Hey guyz if i wanna know how to find size of unsigned char*? void func1(unsigned char* str) { int a; a=?? ////////////////now how to store size of str in a???? }
7
by: Tapas Bose | last post by:
Hello. I want to find size of a dynamically allocated array in my following code : #include <iostream> #include <cstdlib> using namespace std; int main() {
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.