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

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 4752
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
Thank you

Oct 3 '07 #11
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 ?
Define "worth it".

Consider a platform with a 64-bit memory bus. That is, all access
to memory is done 8 bytes* at a time. Consider further that, should
a read to a non-aligned double be performed, this would take two
reads from memory rather than one. Consider further still, that a
write to a non-aligned double would, rather than being just a single
write, need two reads plus two writes. That's at least a 100%
performance hit on reads, and 300% on writes.

Next, consider a similar platform which, rather than allowing the
non-aligned access, instead causes a hardware exception. On such a
platform, this probably means any program using such a construct
would crash. On other platforms, an exception handler takes over
and allows the access to happen, by executing code which reads the
two aligned 8-byte values and pulls out the appropriate bytes to
be assembled and returned. Or, for writes, reads the two values,
combines the bytes as needed, and writes both values back out.
Imagine the performance hit on this platform. It is quite easy to
forsee a 10,000% performace hit or more.

(Note: I have worked on platforms with all three of the above
scenarios, though not necessarily 8-byte alignment. And note, too,
that except for the "crash on non-aligned values" system, the C
compilers I used had an option to change the alignment used.)

So, once again, define "worth it".
* - Okay, so I'm assuming 8-bit bytes here. Sue me.

--
+-------------------------+--------------------+-----------------------+
| 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 3 '07 #12
>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.
I'm not sure I believe there are platforms where it "simply wouldn't
work" in a way the compiler writer couldn't work around (perhaps
very painfully). Accessing a (possibly) misaligned double can be
done by memcpy()ing to a temporary variable that is aligned, and
using the value there. Storing values can be done in reverse: store
to an aligned temporary, then memcpy() to the (possibly) misaligned
location. All this can be done automatically by the compiler. It
will likely cause a HUGE performance penalty, but it can be done.
You also probably get the performance penalty if it MIGHT be
misaligned rather than only if it IS misaligned.

It still comes down to a tradeoff: execution time vs. memory use.
>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.
I think all processors that can implement memcpy() fall in this category,
but it may be the compiler, not the hardware, that determines the speed.
>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.
Oct 3 '07 #13

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

Similar topics

8
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...
18
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
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...
7
by: Luiz Antonio Gomes Picanço | last post by:
I have this structure: struct some { int version; char signature; } some2 void main() {
13
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
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
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...
56
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 ...
15
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
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...
0
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,...

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.