Sign in to follow this  

Unhandled exception at XXX. Datatype misalignment.

This topic is 3871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Iv run into some problems. Im sending packets using sockets. Before i send em, i batch em togethere in a large buffer to keep packet count down. However, when i try to unpack the batch on the receiver side, i get an excepthin thrown "Datatype misalignment.". This happens when using the =(assignment) operator in my matrix classes for its float members. memcpy'ing the entire matrix instead works fine. But i feel there is a problem here that i want to get to the bottom with. MSDN states: STATUS_DATATYPE_MISALIGNMENT Reading or writing to data at an address that is not properly aligned; for example, 16-bit entities must be aligned on 2-byte boundaries. (Not applicable to Intel 80x86 processors.) However i still dont understand why this happens, im not using any pragma pack's in the code, so everything should be aligned nicely right? What im doing is this: char* buffer = socket.recv(); my_packet_type_t* packet = (my_packet_type_t*)buffer; matrix_t my_matrix = packet->m_matrix; // <- BOOOM memcpy(&my_matrix, &packet->m_matrix, sizeof(matrix_t)); // <- works! And then it goes boom. However, this just happens on PowerPC hardware, havent been able to reproduce it on a PC. Neither can i reproduce it locally (ie not sending the data over a socket). So there no problem with What could be wrong? Do i manually need to pad all the data to align or something? That would be odd. Thanks in advance.

Share this post


Link to post
Share on other sites
Is m_buffer dynamically allocated?

I believe that new char[] isn't required to return aligned data, or, at very least, it returns as aligned per char (1 byte boundary).

Also, what does my_packet_type_t look like?

Memory alignment and aliasing can be annoying, this is why it's best to avoid them as much as possible.

One solution would be to define a read float method, then use memcpy in there to read each float individually.

But come to think of it... Doesn't PPC use different byte order altogether, making such reading impossible in the first place?

Share this post


Link to post
Share on other sites
When you are de-marshalling, there is no guarantee that the source data is aligned correctly for the native data type. For example, if you first write a single byte, and then a float, the float in that buffer will be mis-aligned.

What you should do is use memcpy() to de-marshal, rather than the native "=" operation.

Also, if your matrix class is SSE enhanced, then the buffer needs to be 16-byte aligned, and data returned from malloc()/new[] is only, at best, 8-byte aligned.

Share this post


Link to post
Share on other sites

This topic is 3871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this