Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-03-2007, 05:29 PM   #1
Registered: Aug 2006
Posts: 100

Rep: Reputation: 15
isnt it considered to be misaligned data?

hi all,
i have read more about data misalignment but i still have one more question:

suppose i have the following structure:
struct x_
   char a;     // 1 byte
   int b;      // 4 bytes
   short c;    // 2 bytes
   char d;     // 1 byte
} MyStruct;
and the processor can access 4 bytes of memory at once. if the process tries to access the integer filed b it would be misaligned data so the compiler pads this structure to enforce alignment naturally as follows:

0x0000   char a;    
0x0001   char pad0[3]; //this to force data to be aligned.
0x0004   int b;
0x0008   short c;
0x000a   char d;
0x000b   char pad;
i m still confused how the data are not misaligned after this padding? i mean that if the process tries to access the char field named by d : it should access the memory address 0x0008 then making shifting to get access of address 0x000a,right?

so by this the int b is aligned but the char d is misaligned , but every document i read tells that all the data are aligned?

how could this happen, may b i m still confused what is meant by aligned and misaligned data concepts?

thanks in advance
Old 05-03-2007, 06:08 PM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
The answer would be architecture dependent. When working with structures, you also need to consider whether Little vs Big Endian. You may want to use a union which would allow you to access a structure of ints and chars one byte at a time.

You could also write the contents to a file and use OD to examine the structure. This shows why it is more portable to serialize the data and save an ascii representation to the file rather than the structures themselves.
Old 05-03-2007, 06:53 PM   #3
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
Originally Posted by nesta
so by this the int b is aligned but the char d is misaligned , but every document i read tells that all the data are aligned?
It all depends on what you're aligning to. That structure is only misaligned if you're aligning everything to whole word boundaries. But you don't necessarily have to do that. You could also align the smaller data types to half-word boundaries. In that case, the structure is aligned.
Old 05-04-2007, 04:09 AM   #4
Registered: Aug 2006
Posts: 100

Original Poster
Rep: Reputation: 15
so can i say that the data alignment in general means that the processor can access the memory only one time to get the data.right?
Old 05-04-2007, 07:24 AM   #5
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 199Reputation: 199
Is this related to a specific problem or just out of interest?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
misaligned manual pages rsashok Linux - General 4 12-21-2006 01:02 PM
misaligned resource pointer Gordog MEPIS 0 09-30-2006 05:00 AM
Firebird chrome misaligned in Gnome /Fedora qwerty2003_a Linux - Software 4 12-29-2003 12:06 PM
Xerox M750 Printer misaligned Sfrong Linux - Hardware 4 10-02-2003 10:40 PM
WTF? isnt linux come lindows isnt? Cycopath81090 Linux - Newbie 11 08-22-2003 08:19 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration