Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

03
Jan

How Bus Error occurs

Related Blog Items

Today while testing some program I’d encountered this problem. So i’ve digged bit more into it.

The reasons for bus error are

1)accessing unaligned memory addresses: if we are accessing a memory address which is unaligned, this can potentially raise SIGBUS signal on some systems on others this may just slowdown the reading process.

2)accessing a address out of range or non existant address: if the address we are giving is not exist in our enire hardware address range then data bus going to return with error, this will trigger SIGBUS.

3)accessing undefined address: if we try to access undefined address, this may result in segmentation fault

Here is an example which explains the above stuff….

#include <stdio.h>
#include <signal.h>
#include<stdlib.h>

void catchsig(int sig)
{
  if (sig==SIGSEGV)
  {
    printf("segmentation fault\n");
  }
  else if (sig==SIGBUS)
  {
    printf("bus error\n");
  }
}

int main(int argc, char **argv)
{

  char data[sizeof(unsigned long)];
  int *x; int y;

  signal(SIGSEGV, catchsig);
  signal(SIGBUS, catchsig);

  x = (int *)(data + 1);
  y = *x; /*causes bus error on some platforms*/

  x = (int *)0xffffffff /*lets say this address is not in our hardware*/
  y = *x; /*results bus error, as address bus returns with an error*/

  x = (int *)0×100; /*undefined address*/
  y = *x;  /*casues segmentation fault on some machines*/

  return 0;

 

[tags]bus error, SIGBUS, sigmentation fault, sigbus error, sigbus signal, bus errors[/tags]

Popularity: 5%

You need to log on to convert this article into PDF


Related Blog Items

1 Comment

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image