473,804 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

structure padding

Hi all,
I understand that some compilers pad some bytes to the
aggregate data types in order to access the members of the aggregate
data types(i.e. structure) fast. This depends on the architectures.
Some architectures cannot access the data which will be stored on the
odd addresses or they may find difficult to access it. This is the
reason for padding extra bytes.

Now consider the following structure:

struct {
int i; // 4 bytes
char c; // 1 byte
char b; // 1 byte
}a;

Now consider the memory representation for this structure:

----------------------------- Memory locations
i 0
-----------------------------
i 1
-----------------------------
i 2
------------------------------
i 3
-------------------------------
c 4
--------------------------------
b 5
--------------------------------
padding 6
---------------------------------
padding 7
---------------------------------

The size of this strucutre will be 8bytes after padding.
The first 4 memory locations(0 to 3) are allocated for int i;
The next byte(4) is allocated for char c;
the next byte(5) is allocated for char b;

Now my doubt is , does not the processor find it difficult to access
the value of the variable which is stored in odd memory location(ie.
5)? can anyone elaborate on this?

Regards

Feb 2 '06
28 20806
Richard Bos wrote:
"Vladimir S. Oka" <no****@btopenw orld.com> wrote:
ramu wrote:
I understand that some compilers pad some bytes to the
aggregate data types in order to access the members of the aggregate
data types(i.e. structure) fast. This depends on the architectures.
Some architectures cannot access the data which will be stored on the
odd addresses or they may find difficult to access it. This is the
reason for padding extra bytes.


This is off topic here.


Not really. That certain types may require alignments, and that
structures may contain padding for that and other reasons, is perfectly
Standard. Which types happen to require padding on a specific
implementation, and how much, that's off topic here. The principle
itself, though, is on topic.


Agreed, in principle. ;-)

How OP's query sounded to me, however, was more on the off topic side
of it (it asked about how "difficult" it was for the processor). I
might have missed the point, though (it has happened before ;-) ).
Now consider the following structure:

struct {
int i; // 4 bytes
char c; // 1 byte
char b; // 1 byte
}a;

Now consider the memory representation for this structure:


< snip >

<OT>
If such a padding is to take place, it's more likely that both `c` and
`d` will `occupy` a 4-byte memory block, or whichever block is
convenient for the implementation.


Not necessarily. One thing is certain, though: if normal ints aren't
just 4 bytes large, but also require 4-byte alignment, the
implementation must make certain that no ints, including the one in the
second member of an array of these structs, break alignment. Of course
it's possible for the implementation to fudge this in the background by
accessing all struct-member ints as arrays of char, but the best and
perfectly Standard way would be to make the whole struct take 8 bytes: 4
for the int, 1 each for the chars, and 2 for padding to make sure the
next int also ends up on a well-aligned memory address.


Well, yes, of course. (still OT)

I also said "likely", not "will". It really depends on what one means
by `best` (speed, memory efficiency, ...?), and what
implementation/architecture we're talking about. Going by OP's
specification of a 4-byte int, assuming 4-byte alignment, and
architecture that excels in reading 4-bytes at a time, if we want
speed, compiler is likely to do what I described. If we asked for
memory efficiency, it could do what you suggested. I'm sure one could
come up with an architecture where what I just said is not true, but I
think it'd have to be quite weird (which still does not make either of
us wrong).

Cheers

Vladimir

Feb 2 '06 #11
"Rod Pemberton" <do*******@bitb ucket.cmm> writes:
[...]
Okay. Layout can be compiler specific.
Now my doubt is , does not the processor find it difficult to access
the value of the variable which is stored in odd memory location(ie.
5)? can anyone elaborate on this?
No idea. The code generate by DJGPP and OW are radically different. You'll
need to study the disassembly.
If you want to see the exact layout of a structure with different packings,
you can play with this code:

#include <stdio.h>

// Works for DJGPP 2.03 and OW 1.3

[...] #ifdef __DJGPP__
#define HANDLE_PRAGMA_P ACK_PUSH_POP 1
#endif
You never use this macro.
// default alignment
struct {
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
unsigned char b;
} s0;

#pragma pack(push,1)

[snip]

"#pragma pack" is non-standard.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 2 '06 #12
ramu wrote:
[...]
Some architectures cannot access the data which will be stored on the
odd addresses or they may find difficult to access it. This is the
reason for padding extra bytes.

Now consider the following structure:

struct {
int i; // 4 bytes
char c; // 1 byte
char b; // 1 byte
}a;

Now consider the memory representation for this structure:

----------------------------- Memory locations
i 0
i 1
i 2
i 3
c 4
b 5
padding 6
padding 7
--------------------------------- [...] Now my doubt is , does not the processor find it difficult to access
the value of the variable which is stored in odd memory location(ie.
5)? can anyone elaborate on this?


If the processor were unable to access chars at an odd address (highly
unlikely, since this would make accessing character arrays difficult),
then the compiler could always add additional padding:

i: 0, 1, 2, 3
c: 4
pad: 5
d: 6
pad: 7

More likely, it is 16-bit values which require even addresses, and
32-bit values which require either even or multiple-of-4 addresses,
while 8-bit chars can be at any (valid) address.

(Yes, this assumes 8-bit chars. Adjust the above examples as needed
for non-8-bit-char systems.)

Also, note that some architectures may allow no padding at all for
your example, using only 6 bytes for the struct. Still others may
allow it but incur a speed penalty for accessing "unaligned" values.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Feb 2 '06 #13

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Rod Pemberton" <do*******@bitb ucket.cmm> writes:
[...]
Okay. Layout can be compiler specific.
// Works for DJGPP 2.03 and OW 1.3 #ifdef __DJGPP__
#define HANDLE_PRAGMA_P ACK_PUSH_POP 1
#endif
You never use this macro.


Correct. DJGPP needs it to enable '#pragma pack' and '#pragma push'.
// default alignment
struct {
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
unsigned char b;
} s0;

#pragma pack(push,1)

[snip]

"#pragma pack" is non-standard.


Correct again. All packing methods are non-standard. This is the only
method common to both DJGPP and OpenWATCOM. Both DJGPP and OpenWATCOM have
three methods of packing. '...structures. ..' or '...struct data...' will
need to be replaced with valid C:

/* DJGPP */
#ifdef __DJGPP__

//DJGPP Method 1
//--------
#define HANDLE_PRAGMA_P ACK_PUSH_POP 1
#pragma pack(push,1)
....structures. ..
#pragma pack(pop)

//DJGPP Method 2
//--------
#define HANDLE_SYSV_PRA GMA 1
#pragma pack(1)
....structures. ..
#pragma pack()

//DJGPP Method 3
//--------
struct EX
{
...struct.data. ..
} __attribute__ ((packed));

#endif

/* WATCOM */
#ifdef __WATCOMC__

//WATCOM Method 1
//---------
#pragma pack(push,1)
....structures. ..
#pragma pack(pop)

//WATCOM Method 2
//--------
#pragma pack(1)
....structures. ..
#pragma pack(2)

//WATCOM Method 3
//--------
_Packed struct EX
{
...struct.data. ..
};

#endif
Rod Pemberton
Feb 2 '06 #14
"Rod Pemberton" <do*******@bitb ucket.cmm> writes:
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Rod Pemberton" <do*******@bitb ucket.cmm> writes: [...]
> #ifdef __DJGPP__
> #define HANDLE_PRAGMA_P ACK_PUSH_POP 1
> #endif


You never use this macro.


Correct. DJGPP needs it to enable '#pragma pack' and '#pragma push'.

[...]
> #pragma pack(push,1)

[snip]

"#pragma pack" is non-standard.


Correct again. All packing methods are non-standard.

[...]

And therefore off-topic.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 2 '06 #15

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Rod Pemberton" <do*******@bitb ucket.cmm> writes:
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Rod Pemberton" <do*******@bitb ucket.cmm> writes: [...] > #ifdef __DJGPP__
> #define HANDLE_PRAGMA_P ACK_PUSH_POP 1
> #endif

You never use this macro.


Correct. DJGPP needs it to enable '#pragma pack' and '#pragma push'.

[...]
> #pragma pack(push,1)
[snip]

"#pragma pack" is non-standard.


Correct again. All packing methods are non-standard.

[...]

And therefore off-topic.


Vladimir already said he thought the whole thread was off topic, but
apparently a bunch of people disagree with both of you.

Rod Pemberton
Feb 3 '06 #16
"Rod Pemberton" <do*******@bitb ucket.cmm> wrote:

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Rod Pemberton" <do*******@bitb ucket.cmm> writes:
Correct again. All packing methods are non-standard.

[...]

And therefore off-topic.


Vladimir already said he thought the whole thread was off topic, but
apparently a bunch of people disagree with both of you.


Well, yes. I disagree that the _whole_ thread is off topic. The mere
possibility of packing bytes is ISO Standard C. Any implementation-
specific ways of munging them is not, though, and therefore your DJGPP-
and-Watcom-only solution _is_ off topic.

Richard
Feb 3 '06 #17

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:43******** ********@news.x s4all.nl...
"Rod Pemberton" <do*******@bitb ucket.cmm> wrote:

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Rod Pemberton" <do*******@bitb ucket.cmm> writes:
> Correct again. All packing methods are non-standard.
[...]

And therefore off-topic.


Vladimir already said he thought the whole thread was off topic, but
apparently a bunch of people disagree with both of you.


Well, yes. I disagree that the _whole_ thread is off topic. The mere
possibility of packing bytes is ISO Standard C. Any implementation-
specific ways of munging them is not, though, and therefore your DJGPP-
and-Watcom-only solution _is_ off topic.


A) Implementation extensions to C are still C. C is not a static language.
Go study the history of the development of C from an interpreted typeless
language similar to FORTH. Go find out why operators like '=>>', '=+' were
changed to '>>=', '+=' and then come back me and try to claim that the only
C is specification X,Y, or Z.

B) One can't offer a solution without a request. I gifted a useful code
sample out of generosity to one who is learning about packing. Go back and
reread my post. If you can't comprehend _that_ from my post, you shouldn't
be reading.

C) It amazes me that there are so many people _here_ who incessantly whine
about things that are _beyond_ their control, like: judgemental off-topic
posts, top posting, cutting reply signatures. The 'perceived constraints'
that you experience are _you're_ problem. Not mine. Honestly, whining
about these things reminds me of the bickering of immature little children.
Keep it to yourself and learn to deal with it.
Rod Pemberton
Feb 3 '06 #18
Rod Pemberton wrote:
.... snip ...
C) It amazes me that there are so many people _here_ who incessantly
whine about things that are _beyond_ their control, like: judgemental
off-topic posts, top posting, cutting reply signatures. The
'perceived constraints' that you experience are _you're_ problem.
Not mine. Honestly, whining about these things reminds me of the
bickering of immature little children. Keep it to yourself and learn
to deal with it.


However the whining bickering immature children often grow up into
useful adults, provided they are corrected when whining, bickering,
or otherwise acting immature. Here we can only identify the
children by their immature actions, such as top-posting, omitting
context, chopping off attributions, failure to snip.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
Feb 3 '06 #19
"Rod Pemberton" <do*******@bitb ucket.cmm> writes:
[snip]
C) It amazes me that there are so many people _here_ who incessantly whine
about things that are _beyond_ their control, like: judgemental off-topic
posts, top posting, cutting reply signatures. The 'perceived constraints'
that you experience are _you're_ problem. Not mine. Honestly, whining
about these things reminds me of the bickering of immature little children.
Keep it to yourself and learn to deal with it.


Um, no.

There are very good reasons why we try to encourage people to follow
some simple posting guidelines here. Jack Klein did an excellent job
of explaining those reasons. Please read this:

<http://groups.google.c om/group/comp.lang.c/msg/1460ed5b9ad3dae 1?hl=en>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 3 '06 #20

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

Similar topics

13
3896
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char keys; char padding;
2
1857
by: Sachin | last post by:
typdef struct { int i; char ch; }str; str str_var; char x, y; main() { //do nothing
15
2789
by: damian birchler | last post by:
Hi I'm wondering of what type a structure is. Of course, it is a _structure_, but an array isn't an _array_ either. So of what type is a structure? I'd say a pointer, am I right?
10
2315
by: ranjeet.gupta | last post by:
Dear All !! Before i qoute my querry, I will like to qoute my analysis and my Knowledge Struct a { int raw; char data; };
20
2325
by: Lalatendu Das | last post by:
hi let's say i have a structure struct test { int A; char B; int C; }; this above structure defination always going to take 16 byte in memeory in whatever manner we align the member variables while declaring a variable to it . because variable 'A' going to take 4 byte then four charachter of
28
3649
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...
4
11214
by: junky_fellow | last post by:
Can somebody please tell me about the structure alignment rules ? What I found was that on my system (cygwin running on PC, size of int=4 sizeof long=4, size of long long = 8) the cygwin compiler put the padding after the last member of structure. For eg, struct test { int i; char c; /* no padding required between int and char */ /* 3 byte padding is inserted here, Why ? */
24
2131
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
10
3293
by: Rohit kumar Chandel | last post by:
Hi All, Please let me know how to find in a structure whether compiler has used padding or not. Regards Rohit
12
4808
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10318
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...
0
10069
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...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5505
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
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
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.