PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Data Alignment

Suppose you have a struct something like this:

Code:

typedef struct _tagMyStruct {
int a;
char b;
short c;
char d[29];
short e;
} MyStruct;

typedef struct _tagMyStructTwo {
short a;
short b;
MyStruct c[100][150];
short d;
bool e;
char f;
} MyStructTwo;


How would you go about ensuring that these data structures are aligned on DWORD boundaries (other than using #pragma pack (n) or UNALIGNED since those simply don't help in my situation)? As you probably know, datatype misalignment is a major headache in ppc development, especially moving code from DOS to ppc. I've finally been bitten by this problem (why I'd not seen it until today is unexplainable), and am trying to find a solution by reordering my structures and possibly padding them to make them align properly.
This would be better placed in the general dev forum.

But anyway you can set the alignment for an entire project in compiler settings. To align just those structs to a specific boundary you would have to use the pragmas.

To better optimize the structure for more efficient performance you need some redesign. First off _tagMyStruct is 38 bytes, so will be getting some padding for efficient access (if 40 bytes is a DWORD boundary then only 2 additional bytes, about 5% waste). The array with no padding is 570k, I'm not sure why you have that in a struct but I hope you don't declare them often, or at all execept one global. Personally I would just make that global to avoid any possible errors in the future, and maybe wrap it in a namespace, even better than that would be a Singleton class.
Thanks for the reply Sm!rk. After reading it I began thinking about what I was trying to do from a different viewpoint and solved the problem using pragmas. I also found a few bits of code using bools improperly (keep in mind this is 20 year old code for a DOS app, and written for Borland compilers!). These combined, seemed to have fixed up the code correctly.
So which pragmas did you end up using?
#pragma pack (4)
typedef struct __tagName {
...
...
...
} tagName;
#pragma pack ()

This seems to have done the trick. Since this was originally written for DOS, all INTs were considered to be 16-bits, so I needed to code using SHORTs or face tons of problems at the start of the project. This final cleanup only cost me about 36 hours (research, actual work and testing time), so it was a reasonable price to pay.
GR - congrats on your release!

Out of curiousity, how did you figure out your bug was an alignment issue?
I was getting some pretty wild crashes when I hit certain areas of the game, so I ran it on the debugger and happily the debugger said it was a datatype misalignment error. Since Warlords II was originally a DOS app, everything was based on 16 bit ints (SHORTS), not to mention the fact that BOOLS were being used as CHARS and this caused a ton of difficulties. Sm!rk's suggestion made me go back and look at what I was trying to do and helped solve the problem.

Thanks for the congrats too! The game is doing very well on Handango (PocketGear is another story as usual). =)