Bus Error with Unix

Posted on 2004-11-09
Last Modified: 2010-08-05
Good day all . . .

I have an error which is "Bus Error" when trying to run the executable file . . .

The following is the code for both the header file and the executable file . . .

Please help me out . . .

Thanks in advanced . . .

This is the header file:

/* Demo of dynamic data structures in C                      */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define FALSE 0
#define NULL 0

typedef struct listelement{
    long     dataitem;
    struct listelement *link;
}listelement ;

void Menu (int *choice);
listelement * AddItem (listelement * listpointer, int data);
listelement * RemoveItem (listelement * listpointer);
void PrintQueue (listelement * listpointer);
void ClearQueue (listelement * listpointer);
int isEmpty(listelement * listpointer);

int isEmpty(listelement * listpointer)
      if(listpointer -> link == NULL)
            return 1;
            return 0;

listelement * AddItem (listelement * listpointer, int data) {

listelement * lp = listpointer;

    if (listpointer != NULL) {
      while (listpointer -> link != NULL)
          listpointer = listpointer -> link;
      listpointer -> link = (struct listelement  *) malloc (sizeof
      listpointer = listpointer -> link;
      listpointer -> link = NULL;
      listpointer -> dataitem = data;
      return lp;

    else {
      listpointer = (struct listelement  *) malloc (sizeof
      listpointer -> link = NULL;
      listpointer -> dataitem = data;
      return listpointer;

listelement * RemoveItem (listelement * listpointer) {

    listelement * tempp;
    printf ("Element removed is %d\n", listpointer -> dataitem);
    tempp = listpointer -> link;
    free (listpointer);
    return tempp;

void PrintQueue (listelement * listpointer) {

    if (listpointer == NULL)
      printf ("queue is empty!\n");
      while (listpointer != NULL) {
          printf ("%d\t", listpointer -> dataitem);
          listpointer = listpointer -> link;
    printf ("\n");

void ClearQueue (listelement * listpointer) {

    while (listpointer != NULL) {
      listpointer = RemoveItem (listpointer);

AND this is the executable file:

#include <stdio.h>
#include <unistd.h>/* contains fork prototype */
#include "gll.h"

listelement listmember, *listpointer, *temp;
long pid, newPid;

void oneMoreDepth();

main ( )
      listelement listmember, *listpointer, *temp;
      int depth;
      int enteredDepth = 4;
      listpointer = NULL;
      listpointer = AddItem (listpointer, getpid());

      for(depth = enteredDepth; depth < 0; depth--)
            while(isEmpty(listpointer) == 1)

            ClearQueue (listpointer);
            listpointer = temp;
      } // for


void oneMoreDepth()
      newPid = RemoveItem(listpointer)->dataitem;
      pid = fork();
      if(pid == newPid)
            printf ("Parent: %d ID: %d\n", getppid(), getpid()) ;
            pid = getpid(); // in order for its second chiled not to fork
      else if(pid == 0)
            printf ("Parent: %d ID: %d\n", getppid(), getpid());
            temp = AddItem (listpointer, getpid());

      if(pid == newPid)
            pid = fork();
            if(pid == 0)
                           printf ("Parent: %d ID: %d\n", getppid(),getpid());
                  temp = AddItem (listpointer, getpid());
Question by:zaaki
    LVL 12

    Expert Comment

    Hi zaaki,
    Bus errors in Unix normally happen when the CPU tries to dereference a pointer to multi-byte data (short, int, long, etc) using an address which is not a multiple of the data size. Many RISC architectures (such as SPARC) don't like this.

    You can easily create a bus error:

    int main(){
        long l=1;
        long *lp=(long*)((char*)&l + 1);


    Most of these errors are easy to find. Just compile your source with debug info (-g) and run the binary. Once you get a buss error and a core dump, you can use the debugger to see the place where it happened:

    dbx your_binary core

    (Solaris example)


    LVL 12

    Accepted Solution

    Another one is a jump to an odd address:

    #include <stdio.h>

    int run=1;

    typedef int (*main_ptr)();

    int main(){
            main_ptr mp;
            printf("Call %d\n",run++);
            mp = (main_ptr)((char*)main+1);

            return 0;

    Author Comment

    Thank you very much for your responce, but I am a beganer in C Language -I know Java & C#- . . .

    I could not understand the error . . .

    "Segmentation fault" is a new error, and "-1073743228" is printed as the pid (process ID) . . . !!!???

    Please help me to solve these problems since the C Language is used in my Operatin Systems Lab. and this code is a part of a long project which is, as I see, is the only efficient, fast, easier, etc.

    I was workin during my vecation for 5 days day & night to finish this project. On the other hand I have two more projects to be done. More Importantly I have 9 more days before returning back to my university . . . SO, PLEASE HELP ME . . .

    The output of the execution after using gcc with "-o" option is:

    Element removed is 23425
    Element removed is -1073743228
    Segmentation fault

    WHILE the errors after compliling the source code using gcc with "-g" option is:

    pp2(.text+0x1e8):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `PrintQueue'
    /tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:66: first defined here
    /usr/bin/ld: Warning: size of symbol `PrintQueue' changed from 90 to 94 in pp2
    pp2(.rodata+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `_fp_hw'
    /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.rodata+0x0): first defined here
    pp2: In function `_init':
    pp2(.init+0x0): multiple definition of `_init'
    /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o(.init+0x0): first defined here
    pp2(.text+0x248):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `ClearQueue'
    /tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:78: first defined here
    pp2(.text+0x1ac):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `RemoveItem'
    /tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:57: first defined here
    pp2: In function `_start':
    pp2(.text+0x0): multiple definition of `_start'
    /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x0): first defined here
    pp2(.text+0x310):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `oneMoreDepth'
    /tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/pp2.c:35: first defined here
    pp2(.text+0x274):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `main'
    /tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/pp2.c:11: first defined here
    pp2(.fini+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `_fini'
    /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o(.fini+0x0): first defined here
    pp2(.text+0x11c):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `AddItem'
    /tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:33: first defined here
    pp2(.got+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `_GLOBAL_OFFSET_TABLE_'
    /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o(.got.plt+0x0): first defined here
    pp2(.rodata+0x4):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `_IO_stdin_used'
    /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.rodata+0x4): first defined here
    pp2(.text+0x100):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `isEmpty'
    /tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:26: first defined here
    pp2(.data+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `__data_start'
    /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.data+0x0): first defined here
    /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o(.dynamic+0x0): multiple definition of `_DYNAMIC'
    pp2(.dynamic+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: first defined here
    collect2: ld returned 1 exit status

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now