Unix Technical Forum

Data misalignment.

This is a discussion on Data misalignment. within the Sun Solaris Hardware forums, part of the Solaris Operating System category; --> Hi : I have this simple programme: char a,b; int *c=(int *)&b; *c = 0; I know this causes ...


Go Back   Unix Technical Forum > Unix Operating Systems > Solaris Operating System > Sun Solaris Hardware

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-16-2008, 03:58 PM
Prathap
 
Posts: n/a
Default Data misalignment.

Hi :

I have this simple programme:

char a,b;
int *c=(int *)&b;
*c = 0;
I know this causes a bus error on sparc due to data misalignment.

What I want to know is:

What is the idea behind the processor generating an exception like
bus error?
why din't sparc processor just proceed with the read?
why operations on misaligned address is not allowed?

Finally, when I compiled this programme with cc compiler, I did not
get the bus error.
The code compiled with gcc generated bus error!!!

When I looked into the assembly code generated by both the compilers
for this code, they were almost similar.

what caused the code not to generate bus error when compiled with cc
compiler?

Please help me with these questions.

Thanks,
Prathap.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-16-2008, 03:59 PM
Thomas Maier-Komor
 
Posts: n/a
Default Re: Data misalignment.

Prathap wrote:
> Hi :
>
> I have this simple programme:
>
> char a,b;
> int *c=(int *)&b;
> *c = 0;
> I know this causes a bus error on sparc due to data misalignment.
>
> What I want to know is:
>
> What is the idea behind the processor generating an exception like
> bus error?
> why din't sparc processor just proceed with the read?
> why operations on misaligned address is not allowed?
>
> Finally, when I compiled this programme with cc compiler, I did not
> get the bus error.
> The code compiled with gcc generated bus error!!!
>
> When I looked into the assembly code generated by both the compilers
> for this code, they were almost similar.
>
> what caused the code not to generate bus error when compiled with cc
> compiler?
>
> Please help me with these questions.
>
> Thanks,
> Prathap.
>


a bus error signal is only sent if a misalignment in a memory access is
detected. So if you are accessing a 32bit word (int) at an address that
is not 32bit aligned (e.g. 0x???3) you will get SIGBUS. So there is a
chance that you won't get a bus error when accessing a char like an int,
depending on where the char variable is located in memory.

But cc has -xmemalign (see man cc) which is by default set to 8i. It
means all variables get a default alignment of 8 and unaligned access
are interpreted. So you won't get a bus error, but the processor trap
will trigger additional code that handles the situation. It is slow, but
it works.

HTH,
Tom
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:45 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com