At 06:28 AM 9/5/2001 +0000, manish jaggi wrote:
>I am facing a adddress arror when i try executing this code
>please help me out
>#pragma pack (1)
>   struct Record
>{
>         char A[25];
>         Int16 B;
>         Int16 C;
>         char T;
>
>};

[...]

>   Record  *rec = new Record;

[...]

>   int x = rec->B;----------------here the problem is


I don't know why compiler vendors don't warn you about pragmas 
that can get you in trouble.

Of course this causes an address error.  Palms have Motorola 
CPUs, which have two-byte alignment requirements, i.e., any 
memory access for a two-byte or larger value (word, double word, 
float, or pointer) must occur at an even address.  In your 
example, the "new Record" call has produced an even address for 
"rec"; the #pragma directive has insured that no padding occurs 
in struct Record, so the address of rec->B is exactly 25 greater 
than the address of B itself. That puts rec->B at an odd address, 
and you're fetching a 16-bit value from that address.  Kablooey.

You can get rec->B with a little work:

UInt8* p  = (UInt8*)&rec->B;
x = p[1] | (p[0] << 8);


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

Reply via email to