Thread: help needed on debugging "bus error"

  1. #1
    Registered User
    Join Date
    May 2007

    help needed on debugging "bus error"

    Hello. This is my first post here. Thanks for your help.

    I have been coding a program which handles many arrays of strings. The program sometimes (there is a random number generator in the codes) will crash with a message "bus error".

    I am guessing it's caused by those strings. I tried to find out where exactly the error occurs by adding print lines inbetween coding blocks like the following:

    100 printf("100\n");


    200 printf("200\n");


    300 printf("300\n");

    However, it didn't help because the output will be something like


    which means it crashes in the middle of printing 200?

    Anyway, because i can't exactly pin down which part of the code causes the problem, it is very hard for me to fix it.

    Any ideas?

    Thanks a lot.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > there is a random number generator in the codes
    I would avoid calling srand() until you're sure everything works. At least then you'll get some measure of repeatability.

    As for the bus error, that is much harder.

    If you run it inside the debugger, it will trap the BE and stop the program where the problem is first identified. Unfortunately, this is seldom related to the root cause of the problem which is likely to be somewhere else in the code.

    First check that all your pointers are at least initialised to NULL, if you don't initialise them when you declare them. Also check all loops which subscript into arrays, to make sure they're in bounds as well.

    int arr[N]; would be indexed with for ( i = 0 ; i < N ; i++ )
    Starting at 1, or doing <= N are common mistakes.

    If you're still stuck and the code isn't too long, then post it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2007
    Thanks for the reply.
    Indeed it was an array index problem.

    gdb helped me to find the damn line.

    Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "bus error"
    By Bnchs in forum C Programming
    Replies: 2
    Last Post: 05-14-2008, 04:32 PM
  2. help needed in organising a debugging contest
    By shrijeetp in forum A Brief History of
    Replies: 1
    Last Post: 01-22-2006, 07:45 AM
  3. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  4. Replies: 2
    Last Post: 03-21-2004, 08:21 PM
  5. bull, I got a "bus error"
    By jjj in forum C Programming
    Replies: 2
    Last Post: 09-12-2002, 01:12 PM