473,549 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Byte Alignment

Can somebody explain how the byte alignment for structures work,
taking the following example and considering:
byte of 1 Byte
word of 2 Bytes
dword of 4 Bytes

typedef struct
{
byte a;
word b;
dword c;
byte d;
}foo;

foo foo1;
x=sizeof(foo1);

What will be the value of x in different cases,
For One Byte Alignment
For Two Byte Alignment
For Four Byte Alignment
It will be nice if I get a good explaination too...Thanx
Nov 13 '05 #1
4 17676
Shashi <sh***********@ yahoo.com> scribbled the following:
Can somebody explain how the byte alignment for structures work,
taking the following example and considering:
byte of 1 Byte
word of 2 Bytes
dword of 4 Bytes typedef struct
{
byte a;
word b;
dword c;
byte d;
}foo; foo foo1;
x=sizeof(foo1); What will be the value of x in different cases,
For One Byte Alignment
For Two Byte Alignment
For Four Byte Alignment
It will be nice if I get a good explaination too...Thanx


It is entirely possible that x will be 1000 for each case. When
calculating the sizeof of a struct, the C implementation has to
take alignment issues into consideration. However, it isn't required
to use alignment issues as the *only* reason for the sizeof.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
- Douglas Adams
Nov 13 '05 #2
"Shashi" <sh***********@ yahoo.com> wrote:
Can somebody explain how the byte alignment for structures work,
The insertion of padding bytes in structures is entirely up
to each compiler and there is no rhyme nor reason specified
by the C Standard. The only restriction is that there can
be no padding before the first member of the structure. There
can be any number of bytes skipped between each member and
after the last member.
taking the following example and considering:
byte of 1 Byte
word of 2 Bytes
dword of 4 Bytes
Good thing you specified that -- but remember that some
implementations do not have an integer type with exactly
2 bytes or exactly 4 bytes.
typedef struct
{
byte a;
word b;
dword c;
byte d;
} foo;

foo foo1;
x=sizeof(foo1);

What will be the value of x in different cases,


All you can tell is that it will be at least 8 -- it
could be any size greater than or equal to 8.

--
Simon.
Nov 13 '05 #3
Shashi wrote:

Can somebody explain how the byte alignment for structures work,
taking the following example and considering:
byte of 1 Byte
word of 2 Bytes
dword of 4 Bytes

typedef struct
{
byte a;
word b;
dword c;
byte d;
}foo;

foo foo1;
x=sizeof(foo1);

What will be the value of x in different cases,
For One Byte Alignment
For Two Byte Alignment
For Four Byte Alignment
It will be nice if I get a good explaination too...Thanx


The Standard that defines the C language says little
about alignment. It notes that some implementations may
find it advantageous to align certain data objects to
certain address boundaries, and it permits implementations
to insert extra "padding bytes" after struct elements in
order to satisfy the alignment requirements. It can be
deduced that the alignment for some object is a divisor
of the object's size, but this is not explicitly stated.

"One byte alignment" presumably means that a data
object is properly aligned if its address is divisible by
one, or in other words a data object can begin at any
address. In this case there is no compelling reason for
the implementation to insert any padding in a `foo' struct,
and `sizeof(foo)' is probably 1+2+4+1 = 8 bytes.

"Two byte alignment" presumably means that a data
object is properly aligned if its address is divisible by
two, but that objects smaller than two bytes need no
alignment (because of the divisibility criterion). In
this case there will probably be padding after `a' so that
`b' and `c' can begin on even addresses, and more padding
after `d' so the entire struct takes an even number of
bytes (if its size were odd, you couldn't malloc() an
array containing two of them). So the total is probably
1+1+2+4+1+1 = 10 bytes.

You should now be able to work out the four-byte case,
and I'll leave you the pleasure of doing so (in case this
is homework). The interesting thing about this case is
that we know what alignment is required for `dword' and
for `byte', but "four byte alignment" doesn't tell us how
a `word' must be aligned. The two possibilities lead to
two different likely arrangements of padding bytes, and
you should try to find them both.

Note that I've been saying "likely" arrangements and
"probable" sizes, because the Standard grants quite a lot
of freedom to the implementations in this matter. For
example, an implementation is free to align a struct more
strictly than any of its constituent elements, if that
seems convenient to the implementors; the two-byte case
probably gives a size of 10 bytes, but might plausibly
yield 12 or 16 instead. The only way to be sure is to see
what the implementation actually does -- and to realize
that other implementations may do it differently.

--
Er*********@sun .com
Nov 13 '05 #4
Shashi wrote:
Can somebody explain how the byte alignment for structures work,
taking the following example and considering:
byte of 1 Byte
word of 2 Bytes
dword of 4 Bytes

typedef struct
{
byte a;
word b;
dword c;
byte d;
}foo;

foo foo1;
x=sizeof(foo1);

What will be the value of x in different cases,
For One Byte Alignment Offset field
00 a
01 b
03 c
07 d
08

For Two Byte Alignment Offset field
00 a
01 {padding byte length 1}
02 b
04 c
08 d
09 {padding byte length 1}
10

For Four Byte Alignment Offset field
00 a
01 {padding bytes length 3}
04 b
06 {padding bytes length 2}
08 c
12 d
13 {padding bytes length 3}
It will be nice if I get a good explaination too...Thanx

Many compilers will add extra bytes after a field to make
it align to the next boundary. This is what alignment is
with structures.

Some compilers supply a pragma for "packing" structures,
which means that no padding bytes are added. However, it
may not access the fields correctly.

Remember that the size of a structures may not be the
sum of the size of its fields.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #5

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

Similar topics

4
2932
by: Marco | last post by:
Hi I have to read a binary image. Info tell me that it is a binary image without encoding but with RLE. Also i know it is 4 bytes align. What it means ?? How can i read it ?? I red it is for portability whit different microprocessor. is it right ?? How can i perform my 4 bytes in rle decode function when i have to check first byte...
14
1939
by: gamja | last post by:
Hi all. This is my first post on this group. Nice to meet you, cool guys~! I'm on system programming on various embedded systems and understand very well the byte alignment issues. When I write C code, especially design a structure, I pay attention to the order and size of member variables. Because, my boss always says that all variables...
11
3759
by: Taran | last post by:
Hi all, I was wondering how does address alignment to x byte boundary is done. For example, if I say "adjust the requested size to be on a 4-byte boundary" or for that matter 8 byte boundary. How is this adjustment/alignment done? I goolged around alot and wans't able to find how is it done, all it said was what is byte alignment and...
1
2073
by: v.venkatesh | last post by:
Hi, For one of our design, we had introduced a local array of structures which contains the following fields : typedef struct { W_CHAR ussdService; W_CHAR ussdCommand; BOOL isTrue; } NSussdStruct;
12
8089
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses raw MMX and SSE power, the starting pointer MUST be starting at a 16 byte memory boundary. In C++ I allocate more memory than needed, and in a...
1
3169
by: Gajendra | last post by:
How does the byte packing and the byte alignment work in VC++ compiler? What is the effect of #pragma pack(n) on the alignment and byte packing for example while using the structur struc double a char b char c } in a 8 byte packing the sizeof structure is 16. Could anyone explain.
20
3479
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int *)a2; // now a1=a2, a1=a2, etc.
5
3455
by: moni | last post by:
Hey, My buffer contains a short int, some char, and a structure in form of a byte array. Read the string as: TextBox4.Text = System.Text.Encoding.ASCII.GetString(buffer1, 0, 31); Read the int as:
19
4105
by: glchin | last post by:
Does a compiler guarantee that the variable w below is placed on an eight-byte aligned address? void myFunction( long iFreq ) { const double w = two_pi * iFreq; ... ... }
0
7518
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...
0
7446
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...
0
7956
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...
1
5368
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...
0
5087
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
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...
1
1935
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
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
757
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...

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.