Tuesday, February 26, 2008

SIGBUS error

The main cause of SIGBUS error is attempting to access a value larger than a byte at an unaligned address.

Look what wiki says about the bus error:
Most CPUs are byte-addressable, where each unique memory address refers to an 8-bit byte. Most CPUs can access individual bytes from each memory address, but they generally cannot access larger units (16 bits, 32 bits, 64 bits and so on) without these units being "aligned" to a specific boundary, such as 16 bits (addresses 0, 2, 4 can be accessed, addresses from 1, 3, 5, are unaligned) or 32 bits (0, 4, 8, 12 are aligned, all addresses in-between are unaligned). Attempting to access a value larger than a byte at an unaligned address can cause a bus error.

Lets take an example...

struct example1
{
uint32 A;
uint32 B;
uint32 C;
char D;
BYTE E[32];
} EXAMPLE1;

Here, "E" is not aligned. If you move the "D" a the end, or if you add 3 spare bytes after it then problem will be gone.

No comments: