This site is no longer active and is available for archival purposes only. Registration and login is disabled.

datatype misalignment


datatype misalignment

Postby R0B » Jan 5, 2002 @ 5:52pm

Ok, I have a quick question.  What causes a datatype misalignment error?  I have gotten several of them (most of which I have found workarounds for), but I have no idea what causes them, so it is simply guessing when it comes to fixing them.  I you know how to fix one, that information would be very helpful too.
"1011001010 NNNNNNNNNNOOOOOOOOOOOOOOOO!!!!!!" -Bender
User avatar
R0B
got muffins?
 
Posts: 1894
Joined: Jun 22, 2001 @ 12:04pm


Re: datatype misalignment

Postby simonjacobs » Jan 5, 2002 @ 6:10pm

does it just mean things are not DWORD aligned?<br><br>can you give more info on the code that causes the error?<br>
User avatar
simonjacobs
pm Insider
 
Posts: 311
Joined: Nov 27, 2001 @ 4:51pm
Location: London, UK


Re: datatype misalignment

Postby Digby » Jan 5, 2002 @ 6:40pm

A data type misalignment occurs when you try to read or write data to an address in memory that is not evenly divisible by the length of the data type you are reading/writing.<br><br>In the case of a BYTE, you can read or write to any address because all addresses are evenly divisible by 1.  In the case of a WORD you can only read/write to even numbered addresses because a word is 2 bytes long.  In the case of a DWORD you can only read/write to addresses that are evenly divisible by 4 since a DWORD is 4 bytes in length.<br><br>Not all processors have this limitation.  The x86 won't generate an alignment exception, but you'll pay a performance penalty for accessing unaligned data (I don't know if the perf hit is still true with the later Intel chips).  Since the majority of the ports to the Pocket PC are from the PC you can run into this quite a bit, especially when reading from packed data files.<br><br>The compiler can help you out with this if you tell the compiler that the data addressed by the pointer is unaligned.  Look at the docs for __unaligned keyword.  This will allow your app to run, but at a severe performance penalty when accessing the data through the __unaligned pointer.  A better solution is to change your data file or code so that you are no longer attempting to read/write unaligned data.<br><br>Last modification: Digby - 01/05/02 at 15:40:02
Digby
pm Insider
 
Posts: 1011
Joined: Apr 29, 2001 @ 1:53pm


Re: datatype misalignment

Postby BadBazza » Jan 6, 2002 @ 5:43am

I am probably going to get flamed for this but it works for me ;)<br><br>In the header file where you think the misalignment is happening include the statement<br><br>#pragma pack(2)<br><br>I'm not quite sure what it does, but as I said, it works.<br><br>Hope this helps<br>Bad<br><br>PS Feel free to flame me for this, but please include an explantion as to why I should not use this :)
Lost....... Assumed Coding!
User avatar
BadBazza
pm Insider
 
Posts: 81
Joined: Sep 21, 2001 @ 6:26am
Location: Blackpool, UK


Re: datatype misalignment

Postby R0B » Jan 6, 2002 @ 9:33am

My question is, will it slow down the program as much as __unaligned will?Last modification: Rob - 01/06/02 at 06:33:57
"1011001010 NNNNNNNNNNOOOOOOOOOOOOOOOO!!!!!!" -Bender
User avatar
R0B
got muffins?
 
Posts: 1894
Joined: Jun 22, 2001 @ 12:04pm


Re: datatype misalignment

Postby R0B » Jan 6, 2002 @ 9:47am

"1011001010 NNNNNNNNNNOOOOOOOOOOOOOOOO!!!!!!" -Bender
User avatar
R0B
got muffins?
 
Posts: 1894
Joined: Jun 22, 2001 @ 12:04pm


Re: datatype misalignment

Postby simonjacobs » Jan 6, 2002 @ 9:52am

I dont think that would help.<br><br>Why do you need to access that memory as ULONG's, is it really ULONG data or are you just trying to quickly copy 4 chars at a time?<br><br>You need to make sure the memory SomeVar is allocated on 4-byte boundaries. Allocate as:<br><br>ULONG somevar[size];<br><br>
User avatar
simonjacobs
pm Insider
 
Posts: 311
Joined: Nov 27, 2001 @ 4:51pm
Location: London, UK


Re: datatype misalignment

Postby R0B » Jan 6, 2002 @ 10:06am

Simon, it has to be ULONG, because it is the size of the wad file in bytes (or is that bits? but anyways) and you can get some very big wad files.  I was thinking about making it a 2 byte value, but then I'd still have to change it latter.  I just reallocated SomeVar as a ulong, and lets see what happens.
"1011001010 NNNNNNNNNNOOOOOOOOOOOOOOOO!!!!!!" -Bender
User avatar
R0B
got muffins?
 
Posts: 1894
Joined: Jun 22, 2001 @ 12:04pm


Re: datatype misalignment

Postby R0B » Jan 6, 2002 @ 10:19am

"1011001010 NNNNNNNNNNOOOOOOOOOOOOOOOO!!!!!!" -Bender
User avatar
R0B
got muffins?
 
Posts: 1894
Joined: Jun 22, 2001 @ 12:04pm


Re: datatype misalignment

Postby R0B » Jan 6, 2002 @ 10:25am

"1011001010 NNNNNNNNNNOOOOOOOOOOOOOOOO!!!!!!" -Bender
User avatar
R0B
got muffins?
 
Posts: 1894
Joined: Jun 22, 2001 @ 12:04pm


Re: datatype misalignment

Postby simonjacobs » Jan 6, 2002 @ 10:31am

User avatar
simonjacobs
pm Insider
 
Posts: 311
Joined: Nov 27, 2001 @ 4:51pm
Location: London, UK


Re: datatype misalignment

Postby R0B » Jan 6, 2002 @ 10:37am

"1011001010 NNNNNNNNNNOOOOOOOOOOOOOOOO!!!!!!" -Bender
User avatar
R0B
got muffins?
 
Posts: 1894
Joined: Jun 22, 2001 @ 12:04pm


Re: datatype misalignment

Postby R0B » Jan 6, 2002 @ 10:39am

"1011001010 NNNNNNNNNNOOOOOOOOOOOOOOOO!!!!!!" -Bender
User avatar
R0B
got muffins?
 
Posts: 1894
Joined: Jun 22, 2001 @ 12:04pm


Re: datatype misalignment

Postby Dan East » Jan 6, 2002 @ 10:44am

User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Re: datatype misalignment

Postby R0B » Jan 6, 2002 @ 10:47am

"1011001010 NNNNNNNNNNOOOOOOOOOOOOOOOO!!!!!!" -Bender
User avatar
R0B
got muffins?
 
Posts: 1894
Joined: Jun 22, 2001 @ 12:04pm


Next

Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum