By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,920 Members | 1,931 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,920 IT Pros & Developers. It's quick & easy.

struct + char buffer = alignment problem (bus error)

P: n/a
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

Oct 25 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Tue, 25 Oct 2005 14:48:17 -0700, derek.google wrote:
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

Oct 25 '05 #2

P: n/a

<de**********@grog.net> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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
Oct 25 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.