473,748 Members | 4,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Size of a structure : Structure Padding

case 1 :
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
};

case 2:
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
float f // 4
};

According to the rules of structure padding shouldn't the size of the
above structure be 28 bytes (for a 32 bit compiler , I am using DEV-C+
+) ? But I got 32 as the answer , using sizeof . And when I added
"float f" to the structure , I was getting the same answer , 32 .
Well , if 32 is right instead of 28 in the first case , then after
adding a float , shouldn't it become 36 ? But even in case 2 , its
32 . Is there more to structure padding than simple clubbing together
in groups of 4 ? Somebody please shed some light ?

Oct 1 '07 #1
12 4802
On Oct 1, 10:16 am, Kislay <kislaychan...@ gmail.comwrote:
case 1 :
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4

};

case 2:
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
float f // 4

};

According to the rules of structure padding shouldn't the size of the
above structure be 28 bytes
The only rules for structure padding is that padding may occur
anywhere within the structure, except at its beginning. If this
structure was 100 bytes, that would be still OK

But I got 32 as the answer , using sizeof . And when I added
"float f" to the structure , I was getting the same answer , 32 .
Well , if 32 is right instead of 28 in the first case , then after
adding a float , shouldn't it become 36 ?
Not necessarily, the first structure may have had four bytes of
padding, and the second structure may have none
Is there more to structure padding than simple clubbing together
in groups of 4 ?
There is no requirement to "club" together any number of bytes. The
implementation is free to do what it wants, but usually it pads based
on the alignment requirements of the machine.

Regards,
B.

Oct 1 '07 #2
Kislay wrote:
case 1 :
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
};

case 2:
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
float f // 4
};

According to the rules of structure padding shouldn't the size of the
above structure be 28 bytes (for a 32 bit compiler , I am using DEV-C+
+) ?
Which of the two is "the above structure?"

But no: it really doesn't matter. The size of the struct
is the amount of memory the compiler chooses to devote to it.
Different compilers make different choices, because they are
concerned with different constraints -- more generally, with
different "figures of merit."
But I got 32 as the answer , using sizeof .
... which was the correct answer; bravo! (Of course, a
different compiler might have given a different answer, which
would *also* have been the correct answer; bravo!)
And when I added
"float f" to the structure , I was getting the same answer , 32 .
Another right answer! You're on a roll.
Well , if 32 is right instead of 28 in the first case , then after
adding a float , shouldn't it become 36 ? But even in case 2 , its
32 . Is there more to structure padding than simple clubbing together
in groups of 4 ? Somebody please shed some light ?
Hypothesis: On the system you are using at the moment, the
compiler tries to ensure that every double begins at a memory
address that is (if considered as a number) divisible by eight.
If the hypothesis is correct, does it imply anything about the
alignment requirement for a struct that has a double as one of
its members? Further, does the struct's alignment requirement
have any influence on its sizeof? Hint: In `struct s array[2];'
ponder the addresses `&array[0]' and `&array[1]'.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 1 '07 #3
Kislay wrote:
>
case 1 :
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
};

case 2:
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
float f // 4
};

According to the rules of structure padding shouldn't the size of the
above structure be 28 bytes (for a 32 bit compiler , I am using DEV-C+
+) ? But I got 32 as the answer , using sizeof . And when I added
"float f" to the structure , I was getting the same answer , 32 .
Well , if 32 is right instead of 28 in the first case , then after
adding a float , shouldn't it become 36 ? But even in case 2 , its
32 . Is there more to structure padding than simple clubbing together
in groups of 4 ? Somebody please shed some light ?
It looks as if your doubles need to be aligned to an address that
is zero modulo 8. Now explain how an array of two case 1 structs
could do that if the structure size is 28? I.e. they need padding
by an additional 4 bytes. Your float f added just uses that
padding space.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 1 '07 #4
Kislay wrote:
>
case 1 :
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
};

case 2:
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
float f // 4
};

According to the rules of structure padding
Well, the only "rule" is that the compiler can't put padding before
the first element. Beyond that, whatever works is valid. Nothing
in the standard (AFAIK) would forbid padding which put every
element of the above structs on 32-byte boundaries.
shouldn't the size of the
above structure be 28 bytes (for a 32 bit compiler , I am using DEV-C+
+) ? But I got 32 as the answer , using sizeof . And when I added
"float f" to the structure , I was getting the same answer , 32 .
Well , if 32 is right instead of 28 in the first case , then after
adding a float , shouldn't it become 36 ? But even in case 2 , its
32 .
Consider the possibility that your compiler places doubles on 8-
byte boundaries, and because your struct includes a double, it
must place the entire struct on an 8-byte boundary. This would
mean that case 1 would place 4 padding bytes at the end of the
struct, for a total size of 32. It would also mean that in case
2, those same 4 padding bytes would be occupied by "float f", and
no additional padding would be necessary, meaning the size would
still be 32.
Is there more to structure padding than simple clubbing together
in groups of 4 ? Somebody please shed some light ?
By "clubbing together in groups of 4", I assume you mean "aligning
things to 4-byte boundaries". First, nothing says that things
must be aligned on 4-byte boundaries.

Consider:

struct s
{
char c1[3];
char c2[3];
};

On my system, this has a size of 6, and no padding is involved at
all. This is because the char arrays have no alignment issues.
There is no need to add any padding, as c1[] and c2[] can be at
any address.

Also, you seem to think that the padding bytes are part of each
elememt. For example, your comment "8" after "char c1[6]" makes
be think that you believe that c1[] occupies 8 bytes. It does
not. Rather, it occupies 6 bytes, and there are 2 bytes of
padding inserted by the compiler. (Well, 2 bytes may be inserted
as padding by your particular implementation. Nothing requires
that it do so, as far as the standard is concerned.)

So, for your particular implementation, you should really have:

struct s
{
char c1[6]; // 6
// 2 padding bytes
double d; // 8
int i1; // 4
char c2[2]; // 2
// 2 padding bytes
int i2; // 4
// 4 padding bytes
}; // (Total: 32 bytes)

(Disregarding the "// as comments" issue for the sake of clarity.)

+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Oct 1 '07 #5
The individual units of the structure occupy the number of bytes they
are supposed to , and the extra bytes , if any are the padded ones .
So far so good . But the padded bytes will never be free to be used
for anything else , right ? So it can be said that on the whole the
structure is occupying the actual bytes plus the padded ones . The
padded bytes go waste . Consider the following structure ,
struct s
{
char c[2];
double d;
};
c occupies 2 B , d occupies 8 & the structure s occupies 16 (6 padded
bytes) . If these padded bytes cannot be put to any use , isn't it a
big waste of memory . Is structure padding really worth that ?

Oct 2 '07 #6
Kislay wrote:
The individual units of the structure occupy the number of bytes they
are supposed to , and the extra bytes , if any are the padded ones .
So far so good . But the padded bytes will never be free to be used
for anything else , right ?
Not easily. But if you know certain details of your implementation, then
can "use" the space occupied by the padding bytes by reading and writing to
the structure as an array of unsigned char.

This hack however is horribly non-portable and should never be actually
tried.
So it can be said that on the whole the
structure is occupying the actual bytes plus the padded ones . The
padded bytes go waste . Consider the following structure ,
struct s
{
char c[2];
double d;
};
c occupies 2 B , d occupies 8 & the structure s occupies 16 (6 padded
bytes) . If these padded bytes cannot be put to any use , isn't it a
big waste of memory . Is structure padding really worth that ?
It's a matter of trade-off. Aligned objects are often much faster to access
than misaligned ones. Under certain systems objects _have_ to aligned at
certain boundaries. Yes, some space is wasted, but generally the gain in
execution speed is more important than the space consumed.

Do keep in mind that for cases where you *really* need to conserve space,
most implementations have specific switches or "directives " to turn off
structure padding.

Oct 2 '07 #7
On Oct 2, 7:23 pm, Kislay <kislaychan...@ gmail.comwrote:
The individual units of the structure occupy the number of bytes they
are supposed to , and the extra bytes , if any are the padded ones .
So far so good . But the padded bytes will never be free to be used
for anything else , right ?
More or less.
So it can be said that on the whole the
structure is occupying the actual bytes plus the padded ones .
Yes.
The padded bytes go waste .
Whether or not they are 'waste' is a value judgement. The memory they
occupy is not used for any other purpose than padding, certainly.
Consider the following structure ,
struct s
{
char c[2];
double d;};

c occupies 2 B , d occupies 8 & the structure s occupies 16 (6 padded
bytes) . If these padded bytes cannot be put to any use , isn't it a
big waste of memory .
If the code is running on a processor which can only access doubles if
they are on an 8-byte boundary, then it's clearly not a waste at all -
the program simply wouldn't work without it.

If the code is running on a processor which can access doubles much
more efficiently if they are on an 8-byte boundary, but is able to
access them more slowly if they are on another boundary, then whether
or not it is a waste (and how big a waste it is) depends entirely on
how important speed of execution is to you compared to memory size.

If the code is running on a processor which can access doubles equally
efficiently on any boundary, then it would be a big waste - but I
wouldn't expect the compiler to use any padding for that processor.
Is structure padding really worth that ?
In the first case, obviously yes - it's a choice between having the
padding or not having a working program.

In the second case it depends on the tradeoff between performance and
size.

In the third case, no, I'd call a compiler buggy if it used padding
like that in this case (unless there were some other reason to do so).

Oct 2 '07 #8
Kislay wrote:
>
The individual units of the structure occupy the number of bytes they
are supposed to , and the extra bytes , if any are the padded ones .
So far so good . But the padded bytes will never be free to be used
for anything else , right ? So it can be said that on the whole the
structure is occupying the actual bytes plus the padded ones . The
padded bytes go waste . Consider the following structure ,
struct s {
char c[2];
double d;
};
c occupies 2 B , d occupies 8 & the structure s occupies 16 (6 padded
bytes) . If these padded bytes cannot be put to any use , isn't it a
big waste of memory . Is structure padding really worth that ?
Yes, it is necessary. Without it s.d would not be accessible as a
double. That is assuming the needed double alignment is an address
that is 0 modulo 8.

Since the needed alignment for a double is system sensitive, you
can quickly see why the interior structure of a struct is not
portable.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 2 '07 #9
Thank you

Oct 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1848
by: Bryan Feeney | last post by:
This structure is, according to sizeof, 3 bytes long, which makes sense struct test { char text; }; This structure is, according to sizeof, 4 bytes long, which also makes sense struct test { int number;
18
1809
by: Anand Buddhdev | last post by:
Hi everyone, I'm a C newbie, so please be gentle. I have a program that defines the following things: typedef union { unsigned int I; unsigned char b; } dword;
7
2185
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be the size of "ethernet_frame" struct and why ? typedef struct { //This struct defines Arp--Request.
7
2593
by: Luiz Antonio Gomes Picanço | last post by:
I have this structure: struct some { int version; char signature; } some2 void main() {
13
4130
by: luke | last post by:
hi, in Visula C++ 6.0 I have declared a struct like this: typedef struct _WRITE_INPUT { ULONG DeviceNumber; ULONG RegisterNumber; union { USHORT ShortData; UCHAR CharData;
7
1826
by: seema | last post by:
Hi all, I am new to C programming. I have doubt and I want to clarify here is the program , #include <stdio.h> struct windows { int i; char me;
28
3639
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout anyway, so what's the point? Without this restriction the compiler could layout the structure in the most efficient way possible, for some definition of efficient. It would be easy enough to turn this reordering off with a compiler specific...
56
3162
by: Bruce. | last post by:
I would like to allocate a structure size of 1024 bytes but I want the compiler to do the calculation for me. typedef struct { int var1; int var2; int var3; char var4; } MYSTRUCT;
15
2238
by: kris | last post by:
Hi I am writing a small program where I need to obtain the actual size of a structure. The programm is as follows struct abc { int j; char k; int i; }*a;
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.