bus error

R

Rahul

Hi Everyone,

I read an article which claims that an invalid address ( not aligned
in power of two ) would cause a bus error exception, i tried the
following program and it didn't give any error,

int main()
{
int a = 10;
char *p = &a;
int *pp;
printf("%p\n",(void*)&a);
++p;
*p = 10;
pp = p;
printf("pp is %p and val %d\n",pp,*pp);
}

Am i missing something? How do i generate the bus error ?

Thanks in advance ! ! !
 
R

Richard Heathfield

Rahul said:
Hi Everyone,

I read an article which claims that an invalid address ( not aligned
in power of two ) would cause a bus error exception,

It might. It might not. The behaviour is undefined. A bus error is one
legal outcome. The lack of a bus error is another legal outcome.
i tried the
following program and it didn't give any error,

int main()
{
int a = 10;
char *p = &a;
int *pp;
printf("%p\n",(void*)&a);
++p;
*p = 10;
pp = p;
printf("pp is %p and val %d\n",pp,*pp);
}

I got the following diagnostic messages:

foo.c:4: warning: initialization from incompatible pointer type
foo.c:6: warning: implicit declaration of function `printf'
foo.c:9: warning: assignment from incompatible pointer type
foo.c:11: warning: control reaches end of non-void function

Fix the first and third diagnostic messages by changing char *p to int *p.
Fix the second with #include <stdio.h> and remove the ++p to avoid
undefined behaviour for the assignment on the following line. Fix the
fourth message with a return 0; statement just before the program ends.
Remove the final printf.
Am i missing something?

Several things.
How do i generate the bus error ?

By demonstrating your program to an important customer who does not wish to
see a bus error.
 
R

Richard Tobin

Rahul said:
I read an article which claims that an invalid address ( not aligned
in power of two ) would cause a bus error exception, i tried the
following program and it didn't give any error,

You don't really mean "power of two". Processors that require
alignment usually require alignment on a multiple of the object size.

Some processors require alignment, some don't. Presumably you're
using one that doesn't, probably something in the x86 family.

Processors that require alignment do so for efficiency and simplicity
reasons: an aligned object can be read or written in a single access,
and can't cross page boundaries. From the viewpoint of most C
programmers, it has the advantage that it often catches certain errors
sooner than would happen otherwise. On the other hand, it has the
disadvantage of wasting space between objects of different sizes, and
making it hard to match data structures directly to external
representations.

-- Richard
 
U

Ulrich Eckhardt

Rahul said:
I read an article which claims that an invalid address ( not aligned
in power of two ) would cause a bus error exception

That by itself is wrong. Rather, an unaligned access causes a bus error. An
unaligned access is e.g. accessing a 2-byte value on an odd address, but
not accessing a single byte on the same address. However, on the very
popular x86 platform, this error doesn't even come through to the OS, let
alone the program that caused it. Rather, the unaligned access is emulated
by the CPU, and the only way to see it is to watch for loss of performance.
I guess running on an x86 is the reason that you will never see this error.

That said, this behaviour is specific to both the CPU and the OS (it could
also emulate the access transparently in software!), none of which is
actually mandated by the C language, so I would suggest further discussion
in a different newsgroup.

Uli
 
D

David Thompson

You don't really mean "power of two". Processors that require
alignment usually require alignment on a multiple of the object size.
Technically, but the sizes of primitive types almost always are powers
of two: 2, 4, 8, or 16 bytes. (On byte machines, which nowadays are
all 8 bits.) This is the sort of alignment that actually allows
hardware advantages; multiples of 5, while formally similar, would in
practice be worthless and thus not used.
Some processors require alignment, some don't. Presumably you're
using one that doesn't, probably something in the x86 family.
Yes. Or one that has a silent fixup in place, but that's rarer.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
R

Richard Tobin

You don't really mean "power of two". Processors that require
alignment usually require alignment on a multiple of the object size.
[/QUOTE]
Technically, but the sizes of primitive types almost always are powers
of two: 2, 4, 8, or 16 bytes. (On byte machines, which nowadays are
all 8 bits.) This is the sort of alignment that actually allows
hardware advantages;

I wasn't suggesting that there might be non-power-of-two alignment,
but that you can't just use an arbitrary power of two; you have to
use whatever is appropriate to the object in question.
multiples of 5, while formally similar, would in
practice be worthless and thus not used.

Incidentally, 5 byte floats were common on some early microprocessor
systems, but of course they had no alignment restrictions as they
accessed memory in single bytes.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

stack & passing variables 8
BUS ERROR 6
Bus error because of casting? 8
compressing charatcers 35
The cost of the cheapest routes between cities 3
struct inside struct 5
bus error 7
Bus error--not sure why 10

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top