Hi,

I'm having an issue with parsing a file that contains structures that are 
defined with type 'double'. These are 64-bit doubles, in little endian format. 
In my code, I have typdefs of the various structures and sub-structures within 
the file that are defined using fixed size types - SInt32, UInt32, Float64 and 
so on.

I'm using the relevant swapping routines to read the file into my local format. 
Everything is fine when I compile in 32-bit, but fails to work in 64 bit. It's 
acting as if the fields in the structures are misaligned.

Could this be caused by the 64 bit compiler padding out my structures in a 
different way for 64-bit builds? If so is there a way to tell it not to for 
these types?


Here's some example code:

typedef struct
{
        SInt32                  fileCode;                       // 9994, big 
endian
        SInt32                  unused[5];
        UInt32                  fileLength;                     // big endian, 
length expressed as # of 16-bit words, including header
        SInt32                  version;                                // 
little endian, = 1000
        UInt32                  shapeType;                      // all little 
endian from here on
        Float64                 xMin;                           // bbox
        Float64                 yMin;
        Float64                 xMax;
        Float64                 yMax;
        Float64                 zMin;
        Float64                 zMax;
        Float64                 mMin;
        Float64                 mMax;
}
ESRIFileHeader, *ESRIFileHeaderPtr;


typedef struct
{
        SInt32                  recordNumber;           // big endian
        UInt32                  contentlength;          // big endian, length 
expressed as # of 16-bit words
        SInt32                  shapeType;                      // little endian
}
ESRIRecordHeader, *ESRIRecordHeaderPtr;


// this fragment of code is where I do the pointer arithmetic:

                p = (unsigned char*)[_data bytes];
                eof = p + [_data length];
                p += sizeof( ESRIFileHeader );
                
                while( !done && !mShouldAbort )
                {
                        // parse the record header
                        
                        recP = (ESRIRecordHeaderPtr) p;
                        recNum = 
(NSInteger)CFSwapInt32BigToHost(recP->recordNumber);
                        recLength = 
(NSUInteger)CFSwapInt32BigToHost(recP->contentlength) * 2;
                        shapeType = 
(NSInteger)CFSwapInt32LittleToHost(recP->shapeType);
                        
                        NSLog( @"record #%ld, length=%lu, type=%ld", recNum, 
recLength, shapeType );            // displays garbage values in 64-bit build



In the file, the header is followed by a series of records, each of which has 
the header structure shown, and a variable length data portion. I use pointer 
arithmetic to skip past the header, using the sizeof(ESRIFileHeader) to compute 
the size. Even from the very first record, the following header is misaligned, 
indicating that sizeof() gives a different result for 32 versus 64 bit builds. 
Indeed, I get 100 in 32-bits, and 104 in 64-bit builds for the header structure.

That's bad enough, but the values in the Float64 fields of the header appear 
garbage in the debugger in the 64-bit build, so even though I believe I'm 
swapping them correctly, the values returned in the 64-bit version are 
nonsense. I'm less clear about why that should be, but again it could be 
padding - if the padding is not at the end of the structure but somewhere 
before the float values.

Basically, I need to know if there's a way to get the 64-bit compiler to treat 
these structures the same way as it does in a 32-bit build.


--Graham



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to