Bus Error on Integer Assignment

Discussion in 'C++' started by William, Mar 22, 2005.

  1. William

    William Guest

    I have declared the following struct in the .h file:

    struct PeerInfo {
    int peerID;
    string peerIP;
    int peerPort;
    };

    In the .cc file, I have:

    PeerInfo* gossipPeer;
    gossipPeer->peerID = 500; // fail, causes bus error

    If I have:
    gossipPeer->peerID; // pass, no bus error


    My question: why does

    gossipPeer->peerID = 500;

    causes a bus error?
    Any suggestions for a fix is appreciated.

    I am using gcc 3.3.4. The error message simply read "bus error", no
    further details were given.
     
    William, Mar 22, 2005
    #1
    1. Advertising

  2. William

    Jeff Schwab Guest

    William wrote:
    > I have declared the following struct in the .h file:
    >
    > struct PeerInfo {
    > int peerID;
    > string peerIP;
    > int peerPort;
    > };
    >
    > In the .cc file, I have:
    >
    > PeerInfo* gossipPeer;


    That pointer has not been initialized. It holds some quasi-random
    address. You need to obtain some memory, initialize that memory with
    the proper layout for a PeerInfo, and store the address of the memory in
    the pointer:

    PeerInfo* gossipPeer = new gossipPeer;

    When you finish using this PeerInfo object, release the resources used
    to represent it:

    delete gossipPeer;

    > gossipPeer->peerID = 500; // fail, causes bus error


    The junk address in the uninitialized pointer probably had about a 1 in
    4 chance of being properly aligned for an int. Run the old code a bunch
    of times and keep statistics on what % of the time you get bus errors,
    and report back here. I always wondered this sort of thing.


    > If I have:
    > gossipPeer->peerID; // pass, no bus error


    I think that's undefined behavior, but I hope someone better versed than
    I will give a definitive answer.


    > My question: why does
    >
    > gossipPeer->peerID = 500;
    >
    > causes a bus error?


    As opposed to a segmentation fault? gossipPeer probably held a sequence
    of bits that could not possibly represent the address of an int on your
    machine.

    > Any suggestions for a fix is appreciated.


    Instead of using a pointer (PeerInfo*), try allocating gossipPeer "on
    the stack:"

    PeerInfo gossipPeer;
    gossipPeer.peerID = 500;

    This way, you do not have to deal with "new" or "delete."

    > I am using gcc 3.3.4. The error message simply read "bus error", no
    > further details were given.
     
    Jeff Schwab, Mar 22, 2005
    #2
    1. Advertising

  3. "William" <> wrote in message
    news:p...
    >I have declared the following struct in the .h file:
    >
    > struct PeerInfo {
    > int peerID;
    > string peerIP;
    > int peerPort;
    > };
    >
    > In the .cc file, I have:
    >
    > PeerInfo* gossipPeer;
    > gossipPeer->peerID = 500; // fail, causes bus error
    >
    > If I have:
    > gossipPeer->peerID; // pass, no bus error


    My question is, why does that not fault too?
    The answer lies very deep in details no sane
    person would want to have to figure out.

    > My question: why does
    >
    > gossipPeer->peerID = 500;
    >
    > causes a bus error?


    I guess because the pointer refers to some
    "memory" that is not, and the machine knows
    it and let's you know too.

    > Any suggestions for a fix is appreciated.


    Initialize that pointer to refer to an object
    of the claimed type before you use it.

    > I am using gcc 3.3.4. The error message simply read "bus error", no
    > further details were given.


    You're lucky to get that much. Both the read
    (marked "pass") and the write induce undefined
    behavior that could be a bus fault or any number
    of other behaviors, including no "behavior".

    --
    --Larry Brasfield
    email:
    Above views may belong only to me.
     
    Larry Brasfield, Mar 22, 2005
    #3
  4. William

    David White Guest

    "Larry Brasfield" <> wrote in message
    news:AAL%d.33$...
    > "William" <> wrote in message
    > news:p...
    > >
    > > If I have:
    > > gossipPeer->peerID; // pass, no bus error

    >
    > My question is, why does that not fault too?


    Because it doesn't do anything. To any semi-intelligent compiler it's a
    do-nothing statement.

    DW
     
    David White, Mar 22, 2005
    #4
  5. William

    Andre Kostur Guest

    Jeff Schwab <> wrote in news:SPmdnXQBOu_yGKLfRVn-
    :

    >> If I have:
    >> gossipPeer->peerID; // pass, no bus error

    >
    > I think that's undefined behavior, but I hope someone better versed than
    > I will give a definitive answer.


    It might have been optimized away into oblivion... the compiler may have
    determined that that line does nothing...
     
    Andre Kostur, Mar 22, 2005
    #5
  6. * Jeff Schwab:
    >
    > > If I have:
    > > gossipPeer->peerID; // pass, no bus error

    >
    > I think that's undefined behavior, but I hope someone better versed than
    > I will give a definitive answer.


    This was discussed extensively in clc++m a while back, and it's definitely
    undefined behavior (but noo, I'm proba'ly, uh, not better versed, than you).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 22, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eli Bendersky
    Replies:
    9
    Views:
    701
    Allan Herriman
    Apr 7, 2006
  2. Giuseppe

    Re: Character assignment causes bus error

    Giuseppe, Jun 24, 2003, in forum: C Programming
    Replies:
    1
    Views:
    1,529
    Giuseppe
    Jun 24, 2003
  3. ben
    Replies:
    4
    Views:
    633
    Martin Ambuhl
    Jun 26, 2004
  4. Randy Kramer
    Replies:
    12
    Views:
    385
    Robert Klemme
    Oct 25, 2007
  5. d1430
    Replies:
    0
    Views:
    1,025
    d1430
    Mar 20, 2012
Loading...

Share This Page