Bus Error on Integer Assignment

W

William

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.
 
J

Jeff Schwab

William said:
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."
 
L

Larry Brasfield

William said:
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".
 
D

David White

Larry Brasfield said:
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
 
A

Andre Kostur

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...
 
A

Alf P. Steinbach

* Jeff Schwab:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top