
In computing, a bus error is generally an attempt to access memory that the CPU cannot physically address. Bus errors can also be caused by any general device fault that the computer detects. A bus error rarely means that computer hardware is physically broken - it is normally caused by a bug in a program's source code.
There are two main causes of bus errors:
CPUs generally access data at the full width of their data bus at all times. To address bytes, they access memory at the full width of their data bus, then mask and shift to address the individual byte. This is inefficient, but tolerated as it is an essential feature for most software, especially string-processing. Unlike bytes, larger units can span two aligned addresses and would thus require more than one fetch on the data bus. It is possible for CPUs to support this, but this functionality is rarely required directly at the machine code level, thus CPU designers normally avoid implementing it and instead issue bus errors for unaligned memory access.
This is an example of un-aligned memory access, written in the C programming language.
#include <stdlib.h>
int main (void) {
/* iptr is a pointer to an integer, usually 32 or 64 bits in size. It is currently undefined. */
int x, *iptr;
/* cptr is a pointer to a character (the "smallest addressable unit" of the CPU, normally a byte) */
char *cptr;
/* malloc() gives us a valid, aligned memory address. put this in cptr */
cptr = malloc(33);
if (!cptr) return 1;
/* increment cptr by 1. It is now unaligned */
cptr++;
/* it is OK to access individual bytes from unaligned addresses */
x = *cptr;
/* copy cptr into iptr */
iptr = (int *) cptr;
/* on some implementations, this will cause a bus error - accessing more than 1 byte from an unaligned address */
/* (note that dereferencing iptr is generally undefined behaviour in C) */
x = *iptr;
return 0;
}
Why are we here?
All text is available under the terms of the GNU Free Documentation License
This page is cache of Wikipedia. History