Talk:Bus error

From Wikipedia, the free encyclopedia

Contents

[edit] Terminology

Folks, what constitutes a Bus Error and what is a Segmentation Fault is system-dependent. Linux tends to use Segmentation Fault for things that on other systems might trigger a Bus Error. The article should clarify this.

Secondly, the article gives the impression as if unaligned access typically raises a Bus Error to userland. This is not always true. Some architectures (like the 80386) don't raise an exception at all and handle it transparently, and on other systems it might be configurable. For example, on DEC Unix (Tru64) running on an Alpha, the kernel caught the exception and handled it itself. Typically only a message was printed but I think this could be switched off aswell. Unaligned access causing an error is otherwise typical of RISC processors, while classical CISC architectures transparently did the extra fetch (VAX, 80386).

[edit] Example

Ummm... methinks that your C compiler will make things magically work. As the article mentions: alignment is only required on the *machine level*. However, if your compiler is sane, it seems obvious to me that it will simulate the desired functionality.

 iptr = (int *) &cptr[1];


[edit] C vs. Java

The article says:

This kind of problem is more likely to occur when the software is written in the C language than in Java.

That's of course true, but it doesn't make any sense to say it like that, because it doesn't offer any explanation why Java is so much better than C. Also it's stupid to say only Java instead of, for example, "languages that don't directly access memory locations, like Java". Opinions? Jsalomaa 19:52, 29 September 2005 (UTC)

[edit] C vs. Java

I think the same. This sentence have to be removed.

[edit] Huhhh?

What does this line mean? Is it even necessary?

[edit] typ0?

Article says: /* this will cause a bus error - accessing more than bytes from an unaligned address */

Should it be: /* this will cause a bus error - accessing more than one byte from an unaligned address */

[edit] Faulty Example

The example C code compiles and runs properly on my FreeBSD4-STABLE (gcc-2.95.4) machine without encountering a Bus Error. Should just remove it until someone can provide example code that consistently reproduces the problem; or at least explain why this example code magically works in some cases.

  • It works because sizeof(char)==sizeof(int) on your machine I think
  • On my system (AMD Athlon XP + Fedora 7) I also don't get a "Bus Error". It might be because of the x86 CPU architecture, as hinted to my by the following comment in OpenSSL's fips/sha1/fips_md32_common.h: "This gives ~30-40% performance improvement in SHA-256 compiled with gcc [on P4]. Well, first macro to be frank. We can pull this trick on x86* platforms only, because these CPUs can fetch unaligned data without raising an exception." I do get a "Bus error" on HP-UX. Someone knowledgeable on the subject should definitely update the article!

[edit] Shorter example

int main() {
  int a;
  int *ptr; 

  ptr = malloc(7);
  a = (int)ptr;
  a+=1;
  ptr = (int*)a;

  // crash
  a = *ptr;
}

[edit] Another Short Example

Has nothing to do with alignment.

#include <stdio.h>
int
main(void)
{
       char s[10] = "123", *s1 = "456";

       *(s + 1) = '\0';
       printf("s='%s'\n", s);   /* s='1'  */
       *(s1 + 1) = '\0';        /* crash! */
       printf("s1='%s1'\n", s1);

       return 0;
}

The array s[10] is data, and the "123" is stored on the stack.

The value of the pointer s1 is data (and is on the stack), and the "456" can be stored anywhere.