473,508 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

misaligned access clarification

hi
i was doing some tinkering and wrote the following code on a HPUX
#include<stdio.h>
2 int main(){
3 char *buf = malloc( 100 );
4 int a = * ( int * ) ( buf + 13 );
5 }

this thing prompyly gave a bus error
i know that this is an example of misaligned access .. i believe that
the compiler expects to get an aligned address ( 4 bytes in this case )
....and since the address is odd its flaunders.
on Intel machine it works fine
my doubt is :
why does the compiler expect to get an aligned address... why can it
not take a misaligned address? what is main reason for alignment... and
slightly digressing... why is alignment of stack necessary ( gcc
compiler keep doing that some how ).......
a detailed answer will be highly appriciated
thanks in advance
kind egards
rahul

Oct 11 '06 #1
2 7878
ra*******************@gmail.com wrote:
hi
i was doing some tinkering and wrote the following code on a HPUX
#include<stdio.h>
2 int main(){
3 char *buf = malloc( 100 );
4 int a = * ( int * ) ( buf + 13 );
5 }
(I don't know about anyone else, but I hate hate hate those line
numbers. They're visually distracting and get in the way if I
want to cutnpaste code into an editor. I'd cut back just a bit
on the whitespace, too.)
this thing prompyly gave a bus error
Promptly? You didn't even need to run it? That's some environment
you have there ...
i know that this is an example of misaligned access .. i believe that
the compiler expects to get an aligned address ( 4 bytes in this case )
C expects you to know what you're doing. If you take an address and
cast it to int* and dereference it, it's expected to be the adddress
of an int. If I remember PA-RISC correctly (I'm assuming that's what
you're running HPUX on) then ints are aligned on 4-byte boundaries
and misaligned addresses fault. Since the result of malloc is int-aligned,
and 13 isn't a multiple of 4, `buf+13` isn't an int address. BOOM.
...and since the address is odd its flaunders.
I expect the machine is just swauning around.
on Intel machine it works fine
For values of `work` that are not guaranteed by C.
my doubt is :
why does the compiler expect to get an aligned address... why can it
not take a misaligned address?
Let's be clear. The compiler isn't "taking" any address at all: the
compiler isn't there when the fault happens. It's the /machine/
that doesn't like the address. You promised the compiler that the
address `buf + 13` would be int-aligned, and the compiler, being
both stupid and trusting, believed you.
what is main reason for alignment...
Speed. As a consequence of hardware design, an 4-byte value that
is split over interesting hardware boundaries can be slower
to access. (EG it may require multiple reads.) It may be a /lot/
slower to access. So some machine designs disallow it. C isn't
prejudiced against those machines.

Note that there are other machines with a different treatement
of misaligned adresses. Under RISC OS on the Archimedes and
RSIC PCs, if I remember correctly a word access to a misaligned
address resulted in an int value being read from the aligned
(ie & ~3) address and then being /rotated/ so that the low
byte was the byte at the original address. (I think this was
just a consequence of the way the byte-reading hardware worked.)

As far as C is concerned, this is just another allowed
implementation of undefined behaviour. Seems like a more useful
one to me (fx:innocence).
and
slightly digressing... why is alignment of stack necessary ( gcc
compiler keep doing that some how ).......
Speed.

--
Chris "Essen -8 and counting" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Oct 11 '06 #2
>i was doing some tinkering and wrote the following code on a HPUX
>#include<stdio.h>
2 int main(){
3 char *buf = malloc( 100 );
4 int a = * ( int * ) ( buf + 13 );
5 }

this thing prompyly gave a bus error
i know that this is an example of misaligned access .. i believe that
the compiler expects to get an aligned address ( 4 bytes in this case )
...and since the address is odd its flaunders.
on Intel machine it works fine
my doubt is :
why does the compiler expect to get an aligned address... why can it
Because the *HARDWARE* expects to get an aligned address (for some
processor types). You saw what happened when it didn't. Another
possibility is that you quietly get the wrong bytes (the low-order few
bits of the address may be just *ignored*).
>not take a misaligned address? what is main reason for alignment... and
slightly digressing...
There doesn't have to be a single main reason for alignment. An
important reason is that if your hardware requires it, your program
will crash or you'll fetch the wrong data if you don't align
correctly. Another issue is that even if your hardware doesn't
actually require it, aligned accesses may be faster (it doesn't
require multiple memory fetches to get a single value).
>why is alignment of stack necessary ( gcc
compiler keep doing that some how ).......
a detailed answer will be highly appriciated
On some processors, alignment is not necessary (just faster).

Oct 11 '06 #3

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

Similar topics

35
2292
by: Gabriel Zachmann | last post by:
Is there any generic way to use C++ libraries from within Python. I seem to recall that there are tools to generate wrappers for C-libraries semi-automatically. But those were still way too...
63
5826
by: Jerome | last post by:
Hi, I'm a bit confused ... when would I rather write an database application using MS Access and Visual Basic and when (and why) would I rather write it using Visual Studio .Net? Is it as easy...
11
17018
by: Rosco | last post by:
Does anyone have a good URL or info whre Oracle and Access are compared to one another in performance, security, cost etc. Before you jump on me I know Oracle is a Cadillac compared to Access the...
1
2838
by: Matt Alanzo | last post by:
On another newsgroup an Access knowledgable party posted: >You should be able to connect an Access ADP to an existing SQLExpress >database running in SQLS 2000 compatibility mode. The only thing...
4
1847
by: chanchito_cojones | last post by:
Hi there, I was searching the net for some guidance in putting together a query that would select random records from the main table. I came across this and it works like a charm. SELECT TOP...
46
3788
by: Adam Turner via AccessMonster.com | last post by:
If I had a field called "Name" in an Access table "Contact Info", and the field contained VBScript... Function Main(rstFields) Main = rstFields.Item("FirstName").Value End Function 1. How do...
2
2187
by: Tim | last post by:
Hi, I have created some code for printing a column report. The default page size is 850 x 1100, which is correct. The print preview looks perfect. The data is positioned exactly where it should...
1
2059
by: vbarookie | last post by:
I was able to consolidate about 100 csv files into one master table which I then imported into access (2003). The problem is that the csv files contained different layouts. Currently the data is...
2
1925
by: sukatoa | last post by:
I doubt if this is the right section to post... But, for a clarification I always use "jdbc:mysql://localhost:3306/dbname" to access the mysql database server given also the username and the...
0
7224
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,...
0
7380
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...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
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...
0
3192
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...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.