struct + char buffer = alignment problem (bus error)

D

derek.google

I have an application that's crashing because of an alignment problem,
and this is the smallest program that demonstrates what's happening:

int main() {
struct Message {
unsigned short size;
};
const int START_INDEX = 1;
char* buffer = new char[1024];
Message* msg = (Message*)&buffer[START_INDEX];
unsigned short s = msg->size;
delete[] buffer;
}

This program dumps core (Bus Error) when START_INDEX is 1, 3, 5, etc.,
but it doesn't crash when START_INDEX is 0, 2, 4, etc.

This only happens when I compile and run on SPARC/Solaris. (I've tried
compiling with GCC 3.4 and Sun Forte and both produce the same
behavior, so I don't think I can fault the compiler.) The program
doesn't crash for any value of START_INDEX on x86/Windows with VC7.

I thought C++ guaranteed that a dynamically allocated array (i.e. char*
buffer) was suitable alignment-wise to hold any structure (i.e. struct
Message). Clearly that's not the case here, because I can only access
the unsigned short msg->size on word boundaries (i.e. START_INDEX = 0,
2, 4, etc.).

Can someone explain what's going on here?

Derek
 
J

Jay Nabonne

I have an application that's crashing because of an alignment problem,
and this is the smallest program that demonstrates what's happening:

int main() {
struct Message {
unsigned short size;
};
const int START_INDEX = 1;
char* buffer = new char[1024];
Message* msg = (Message*)&buffer[START_INDEX];
unsigned short s = msg->size;
delete[] buffer;
}

This program dumps core (Bus Error) when START_INDEX is 1, 3, 5, etc.,
but it doesn't crash when START_INDEX is 0, 2, 4, etc.

Makes sense.
This only happens when I compile and run on SPARC/Solaris. (I've tried
compiling with GCC 3.4 and Sun Forte and both produce the same
behavior, so I don't think I can fault the compiler.) The program
doesn't crash for any value of START_INDEX on x86/Windows with VC7.

It's processor-specific. Some care about alignment, some don't (or degrade
gracefully but still work).
I thought C++ guaranteed that a dynamically allocated array (i.e. char*
buffer) was suitable alignment-wise to hold any structure (i.e. struct
Message). Clearly that's not the case here, because I can only access
the unsigned short msg->size on word boundaries (i.e. START_INDEX = 0,
2, 4, etc.).

I don't follow. How does the fact that &buffer[1] isn't aligned for
Message negate the fact that &buffer[0] is? Your only guarantee is that
the start of the array is aligned. If you start marching off into the
array a byte at a time, you're pretty much guaranteed to misalign at some
point.

Or did you mean:

Message* msg = ((Message*)&buffer)[START_INDEX];

?

- Jay
 
M

Mike Wahler

I have an application that's crashing because of an alignment problem,
and this is the smallest program that demonstrates what's happening:

int main() {
struct Message {
unsigned short size;
};
const int START_INDEX = 1;
char* buffer = new char[1024];
Message* msg = (Message*)&buffer[START_INDEX];
unsigned short s = msg->size;
delete[] buffer;
}

This program dumps core (Bus Error) when START_INDEX is 1, 3, 5, etc.,
but it doesn't crash when START_INDEX is 0, 2, 4, etc.

This only happens when I compile and run on SPARC/Solaris. (I've tried
compiling with GCC 3.4 and Sun Forte and both produce the same
behavior, so I don't think I can fault the compiler.) The program
doesn't crash for any value of START_INDEX on x86/Windows with VC7.

I thought C++ guaranteed that a dynamically allocated array (i.e. char*
buffer) was suitable alignment-wise to hold any structure (i.e. struct
Message). Clearly that's not the case here, because I can only access
the unsigned short msg->size on word boundaries (i.e. START_INDEX = 0,
2, 4, etc.).

Can someone explain what's going on here?

You're not aligning your structure to the *start* of the
allocated memory.

Array indices begin with zero (0), not one (1).

const int START_INDEX = 0;

-Mike
 

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