On Tue, Jan 08, 2002 at 11:07:07AM -0800, Ken Bloom wrote:
> Bus errors are a platform dependant thing. They can be caused by problems such as
>the integer
> alignment error below, where the computer tries to read a long from a memory
>location
> where the processor does not like to find the first byte of a long. This program
>didn't
> cause a bus error on my own x86, however it did cause a bus error on one of the HPs
>in the CS
> lab.
>
> #include <iostream>
#include <iomanip>
> using namespace std;
>
> int main()
> {
> char * x = new char[16]; // the first byte has suitable alignment for anything
>
> for (int t=0;t<16;t++) x[t]=t;
>
> cout << reinterpret_cast<long*> (x) << endl;
> cout << reinterpret_cast<long*> (x+1) << endl;
>
> long a = *(reinterpret_cast<long*> (x+1));
> // there are no guarantees about the alignment of the second byte of data
> // so we could get a bus error here
>
> cout << setbase(ios::hex) << a << endl;
>
> delete[] x;
> return 0;
> }This sucessfully produced an unaligned trap on my Alpha :-). The hardware trap such unaligned accesses, and the kernel emulates them with some performance penalty. This allows badly-written software to keep running. -- Henry House The attached file is a digital signature. See <http://romana.hajhouse.org/pgp> for information. My OpenPGP key: <http://romana.hajhouse.org/hajhouse.asc>.
msg01022/pgp00000.pgp
Description: PGP signature
