tags:

views:

105

answers:

2

I would like to understand why I am getting a bus error with this code.

int main()
{
int p=34;
int *pp= (int *) ((char *)&p+1);
cout<<*pp<<"\n";
return 0;
}
+8  A: 

It will no doubt be an alignment issue. On many architectures, certain types have to be aligned properly, an example being that 4-byte integers must start on a 4-byte boundary.

When you create the integer p, it will be aligned correctly on the stack at an address which is a correct multiple.

By moving that address up on byte, and de-referencing that as an int, you're causing the SIGBUS.

This link at Sun shows the aignment requirements. In short:

  • short integers are aligned on 16-bit boundaries.
  • int integers are aligned on 32-bit boundaries.
  • long integers are aligned on 64-bit boundaries for SPARC systems.
  • long long integers are aligned on 64-bit boundaries.
paxdiablo
Thanks this helped a lot! I understand what they mean by non-aligned address now.
tuckster
+1  A: 

16-bit quantities must be stored at 16-bit or 2-byte alignment, and 32-bit (4 bytes) at addresses which are a multiple of 4.

Many CPUs support unaligned access, but it costs extra circuitry in the chip, and extra execution time to run the extra memory bus cycles to pick up the odd bytes. It is a particularly common RISC processor philosophy that requiring more care on the part of compilers and programmers to lay out data carefully for increased speed and simpler circuitry is an acceptable trade off.

By the way, it's unlikely that a low address like that would be in valid memory anyway. But your example does illustrate that the alignment exception takes priority over the SEGFAULT exception.

wallyk