[vox-tech] what is a bus error

Henry House vox-tech@lists.lugod.org
Tue, 8 Jan 2002 11:30:51 -0800


--SUOF0GtieIMvvwua
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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=20
> alignment error below, where the computer tries to read a long from a mem=
ory location=20
> where the processor does not like to find the first byte of a long. This =
program didn't=20
> cause a bus error on my own x86, however it did cause a bus error on one =
of the HPs in the CS=20
> lab.
>=20
> #include <iostream>
#include <iomanip>
> using namespace std;
>=20
> int main()
> {
>   char * x =3D new char[16]; // the first byte has suitable alignment for=
 anything
>=20
>   for (int t=3D0;t<16;t++) x[t]=3Dt;
>=20
>   cout << reinterpret_cast<long*> (x) << endl;
>   cout << reinterpret_cast<long*> (x+1) << endl;
> =20
>   long a =3D *(reinterpret_cast<long*> (x+1));=20
>   // there are no guarantees about the alignment of the second byte of da=
ta
>   // so we could get a bus error here
>=20
>   cout << setbase(ios::hex) << a << endl;
>=20
>   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.

--=20
Henry House
The attached file is a digital signature. See <http://romana.hajhouse.org/p=
gp>
for information.  My OpenPGP key: <http://romana.hajhouse.org/hajhouse.asc>.

--SUOF0GtieIMvvwua
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8O0jrKK1cAVjXujwRAjnjAKCLPVbt01C8atVvk8rve37eNCkTxgCgv1D2
LBnLqwn+RBhnsu2khw9pX3g=
=i1MP
-----END PGP SIGNATURE-----

--SUOF0GtieIMvvwua--