Page 1 of 3
datatype misalignment

Posted:
Jan 5, 2002 @ 5:52pm
by R0B
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.
Re: datatype misalignment

Posted:
Jan 5, 2002 @ 6:10pm
by simonjacobs
does it just mean things are not DWORD aligned?<br><br>can you give more info on the code that causes the error?<br>
Re: datatype misalignment

Posted:
Jan 5, 2002 @ 6:40pm
by Digby
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
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 5:43am
by BadBazza
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

Re: datatype misalignment

Posted:
Jan 6, 2002 @ 9:33am
by R0B
My question is, will it slow down the program as much as __unaligned will?Last modification: Rob - 01/06/02 at 06:33:57
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 9:47am
by R0B
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 9:52am
by simonjacobs
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>
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 10:06am
by R0B
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.
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 10:19am
by R0B
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 10:25am
by R0B
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 10:31am
by simonjacobs
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 10:37am
by R0B
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 10:39am
by R0B
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 10:44am
by Dan East
Re: datatype misalignment

Posted:
Jan 6, 2002 @ 10:47am
by R0B