473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

struct + char buffer = alignment problem (bus error)

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
2 2995
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

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
56
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
36
by: Eric Laberge | last post by:
Hi! I'm working on automatically generated code, and need to assign arrays. memcpy is an obvious solution, but it becomes complicated to use in the context I'm working on, ie.: I could use it...
8
by: eagle_jyjh | last post by:
For example: the msg = temp_buf; is alwawys ok? //test_msg.cpp struct msg_head { char a01;
11
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a...
6
by: Jack | last post by:
Hi, All, is it possible to send a struct using the the send function. Here is what I mean typedef struct{ int ID; char name; }sampleStruct; int main(){
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.