In article <114250@palm-dev-forum>, [EMAIL PROTECTED] wrote:

> Hai folks,
>    I have a basic doubt about the memory allocated for a structure in Palm.I
> declared following structure in my program. The size of elements in the
> structure is 25 bytes. But when i tried to find size of whole structure
> using sizeof(mystruct) its returning 26 bytes. I found that if single byte
> char datatype is there in the structure this extra byte is comming. Some
> where in the headers of palmos its mentioned that elements in a struct
> follows 16 bit alignment. I am guessing that this interbyte/word alignment
> issue. can anybody explain abit more on this.
<snip>
> 
> typedef struct 
> {
>       Char c_Apln_no[10];
>       Char Dt_AplnDt[12];
>       char C_ChngApld;
>       UInt16 I_RegYear;
> }Do_Apln;

I_RegYear is a UInt16. For this reason, it must start on a 16-bit 
boundry.

Otherwise, you'd find something like this would crash with an address 
error on pre Palm OS 5 devices:

  regYear = aplnP->I_RegYear;

(Actually, I'm not sure if something that simple would crash or not. I 
know I've gotten crashes from only slightly more complicated statements 
with pretty similiar structures.)

If you reverse the C_ChngApld and I_RegYear fields you will get (I 
think!) a 25 byte structure:

typedef struct 
{
 Char c_Apln_no[10];
 Char Dt_AplnDt[12];
 UInt16 I_RegYear;
 char C_ChngApld;
}Do_Apln;

This way, I_RegYear will fall on an even byte. C_ChngApld doesn't have 
any special requirements.  

You could also add a pad byte:

typedef struct 
{
 Char c_Apln_no[10];
 Char Dt_AplnDt[12];
 char C_ChngApld;
 char pad;
 UInt16 I_RegYear;
}Do_Apln;

Then, at least, it would be 26 bytes on both.

There are some interesting implications to this if you end up working 
with a structure that is a variable offset from the start of the 
pointer. If your offset into the structure is odd, all the fields that 
should have been even will be odd as well. I'm not really an expert on 
this. What I know, I learned from crashing. :)

As if this weren't confusing enough, 32 bit integers must be 32 bit 
aligned. Not that you have one in the above strucutre.

> Note: I found that on desktops(windows) this stucture size is correct. On
> plam only its showing difference in size of struct.

Don't take this the wrong way, but I really encourage you to shed the 
"correct" and "incorrect" spin. Like a lot of cross platform work, 
neither approach is right or wrong... they're just different. If your 
structure were to compile the way you think is correct, you'd have the 
potential to crash at runtime. That's obviously not correct behavior! :)

In fact, aligning a 2 byte element on an odd byte seems to me to be a 
fairly bad thing for a compiler to do. Although it's probably a setting 
somewhere.

Hopefully nothing here is too wrong. I welcome any/all corrections... 
this sort of thing has bit me in the past. :)

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to