struct + char buffer = alignment problem (bus error)

Discussion in 'C++' started by derek.google@grog.net, Oct 25, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Jay Nabonne Guest

    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
    Jay Nabonne, Oct 25, 2005
    #2
    1. Advertising

  3. Mike Wahler Guest

    <> wrote in message
    news:...
    >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
    Mike Wahler, Oct 25, 2005
    #3
    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. Artie Gold
    Replies:
    0
    Views:
    500
    Artie Gold
    Aug 21, 2003
  2. Ron Natalie
    Replies:
    3
    Views:
    5,806
    Artie Gold
    Aug 21, 2003
  3. Mike Wahler
    Replies:
    2
    Views:
    485
    Ron Natalie
    Aug 22, 2003
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
  5. Tzury Bar Yochay
    Replies:
    4
    Views:
    566
    John Machin
    Sep 21, 2008
Loading...

Share This Page