473,418 Members | 2,036 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,418 software developers and data experts.

structures

why the size of structures using sizeof is not same as the total sum of different atrributes.
Sep 11 '06 #1
1 2480
Banfa
9,065 Expert Mod 8TB
Because of structure packing. many processors required that variables of a given byte size start on a given byte boundary. Often the boundary is the number of bytes in a the variable up to a given size (often 4) so

1 byte size variables have to start on a 1 byte boundary (i.e. any boundary)
2 byte size variables have to start on a 2 byte boundary address%2 = 0
4 or greater byte size variables have to start on a 4 byte boundary address%4 = 0

This is only 1 scheme there are many schemes which will be different for different processors.

Also generally a structure needs to start on the largest available byte boundary (4 in the above case) to maintain the alignment of it's members, therefore in order to facilitate having a array of structures the structure must end on the required boundary and if necessary padding bytes are added to the end of the structure.

consider these 2 structures

Expand|Select|Wrap|Line Numbers
  1. struct tag_s1 {
  2.     char c;
  3.     long l1;
  4.     short s;
  5.     long l2;
  6. } s1;
  7.  
  8. struct tag_s2 {
  9.     long l1;
  10.     long l2;
  11.     short s;
  12.     char c;
  13. } s2;
  14.  
Assuming sizeof long == 4 and sizeof short == 2 then

In s1

l1 needs to be 4 byte aligned so there will be 3 padding bytes following c. After l1 we are already 4 byte aligned so s can follow immediately but l2 needs to be 4 byte aligned so there will be 2 padding bytes following s. After l2 we are already 4 byte aligned so there is no need for any padding at the end of the structure.

sizeof(s1) = 16 bytes

In s2

l1 finishes on a 4 byte boundar so l2 can follow on immediately without any padding. l2 finishes on a 4 byte boundary so s can follow on immediately without any padding. c can follow immedately because generally you can put a single byte anywhere, after c we are at byte 11 so to get back to a 4 byte boundary 1 padding byte needs to be added at the end of the structure.

sizeof(s2) = 12 bytes


So s1 and s2 hold the same data but s2 does it in 4 less bytes. It is not uncommon in applications where memory is an issue to find structures ordered by the size of there members, largest to smallest, as this generally reduces the amount of memory required for a given structure.

I recently saved over 20k of memory in an embedded application by altering the order of members in a structure to save 12 bytes of padding. This structure was then declared in an array of 1800 items.
Sep 11 '06 #2

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

Similar topics

1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
33
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
14
by: pmclinn | last post by:
I've noticed that many programmers use classes to store data about such things like: Class Customers .....Phone ....ID ....Address End Class....
2
by: thomasfarrow | last post by:
At work, our development team has a development standards document that insists Structures should never be used. I'm looking to change this standard but need a suitable argument in order to make...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
8
by: Bob Altman | last post by:
Hi all, I have a structure that includes a constructor. I want to add a bunch of these structures to an STL map (whose index is an int). If I define the map like this: map<int,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.