Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Bus error
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Example== This is an example of unaligned memory access, written in the [[C (programming language)|C programming language]] with [[AT&T syntax|AT&T assembly syntax]]. <syntaxhighlight lang="c"> #include <stdlib.h> int main(int argc, char **argv) { int *iptr; char *cptr; #if defined(__GNUC__) # if defined(__i386__) /* Enable Alignment Checking on x86 */ __asm__("pushf\norl $0x40000,(%esp)\npopf"); # elif defined(__x86_64__) /* Enable Alignment Checking on x86_64 */ __asm__("pushf\norl $0x40000,(%rsp)\npopf"); # endif #endif /* malloc() always provides memory which is aligned for all fundamental types */ cptr = malloc(sizeof(int) + 1); /* Increment the pointer by one, making it misaligned */ iptr = (int *) ++cptr; /* Dereference it as an int pointer, causing an unaligned access */ *iptr = 42; /* Following accesses will also result in sigbus error. short *sptr; int i; sptr = (short *)&i; // For all odd value increments, it will result in sigbus. sptr = (short *)(((char *)sptr) + 1); *sptr = 100; */ return 0; } </syntaxhighlight> Compiling and running the example on a [[POSIX]] compliant OS on [[x86]] demonstrates the error: <syntaxhighlight lang="console"> $ gcc -ansi sigbus.c -o sigbus $ ./sigbus Bus error $ gdb ./sigbus (gdb) r Program received signal SIGBUS, Bus error. 0x080483ba in main () (gdb) x/i $pc 0x80483ba <main+54>: mov DWORD PTR [eax],0x2a (gdb) p/x $eax $1 = 0x804a009 (gdb) p/t $eax & (sizeof(int) - 1) $2 = 1 </syntaxhighlight> The [[GDB]] [[debugger]] shows that the [[constant (programming)|immediate value]] 0x2a is being stored at the location stored in the [[IA-32|EAX]] [[processor register|register]], using [[X86 assembly language]]. This is an example of [[addressing mode#Register indirect|register indirect]] addressing. Printing the [[least significant bit|low order bits]] of the address shows that it is not [[data structure alignment|aligned to a word boundary]] ("dword" using x86 terminology).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)