LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-03-2007, 05:29 PM   #1
nesta
Member
 
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:
Code:
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:

Code:
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
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
Quote:
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
nesta
Member
 
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
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

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


Reply


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 free...how come lindows isnt? Cycopath81090 Linux - Newbie 11 08-22-2003 08:19 PM


All times are GMT -5. The time now is 07:57 PM.

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