What does "BUS_ADRALN - Invalid address alignment" error means?

Hemant picture Hemant · Jul 14, 2010 · Viewed 12.5k times · Source

We are on HPUX and my code is in C++. We are getting

BUS_ADRALN - Invalid address alignment

in our executable on a function call. What does this error means? Same function is working many times then suddenly its giving core dump. in GDB when I try to print the object values it says not in context. Any clue where to check?

Answer

Omnifarious picture Omnifarious · Jul 14, 2010

You are having a data alignment problem. This is likely caused by trying to read or write through a bad pointer of some kind.

A data alignment problem is when the address a pointer is pointing at isn't 'aligned' properly. For example, some architectures (the old Cray 2 for example) require that any attempt to read anything other than a single character from memory only occur through a pointer in which the last 3 bits of the pointer's value are 0. If any of the last 3 bits are 1, the hardware will generate an alignment fault which will result in the kind of problem you're seeing.

Most architectures are not nearly so strict, and frequently the required alignment depends on the exact type being accessed. For example, a 32 bit integer might require only the last 2 bits of the pointer to be 0, but a 64 bit float might require the last 3 bits to be 0.

Alignment problems are usually caused by the same kinds of problems that would cause a SEGFAULT or segmentation fault. Usually a pointer that isn't initialized. But it could be caused by a bad memory allocator that isn't returning pointers with the proper alignment, or by the result of pointer arithmetic on the pointer when it isn't of the correct type.

The system implementation of malloc and/or operator new are almost certainly correct or your program would be crashing way before it currently does. So I think the bad memory allocator is the least likely tree to go barking up. I would check first for an uninitialized pointer and then bad pointer arithmetic.

As a side note, the x86 and x86_64 architectures don't have any alignment requirements. But, because of how cache lines work, and for various other reasons, it's often a good idea for performance to align your data on a boundary that's as big as the datatype being stored (i.e. a 4 byte boundary for a 32 bit int).