04-29-2008 03:21 PM
04-29-2008 05:57 PM
04-30-2008 04:23 AM
That's not quite correct. Using the default (8 byte) alignment, according to your explanation where every member is 8-byte aligned, then a simple structure with 2 ints would have a size of 16 bytes. A simple experiment shows that this is not the case. The specified alignment is actually on the overall structure itself: in the case of a structure also being a member (of another structure) then yes, that particular member is aligned to 8 bytes, but this is not a general rule for all members.
JR
04-30-2008 07:39 AM
Thanks,
I found the pack pragma and it works just fine.
But this exposed another oddity. I calloc'ed the struct, accounting for the array at the end (2124 elements). I passed this to fwrite with the same size I had passed to calloc, however, fwrite reported that the array was too small 34000 bytes versus 34024 bytes (2124 * 16 + 40).
Hurst
04-30-2008 11:33 AM
04-30-2008 01:20 PM
First of all, I used pack(1)
Here is the code that has the fwrite problem (error line has '>' marker at start of line):
typedef struct MultiPointMRecord_A
{
int ShapeType;
Box Bounds; // Bounding box
int NumPoints;
Vertex Parray[]; // Array of Point's
} MultiPointMRecord_A;
typedef struct MultiPointMRecord_B
{
double Mmin;
double Mmax;
double Marray[]; // Array of M's
} MultiPointMRecord_B;
int i, j, row, col;
int arraySize;
int recordSize_A;
int recordSize_B;
long shpPos = ftell (shpStream);
float height = 0;
float minM, maxM;
MultiPointMRecord_A *pRecord_A = NULL;
MultiPointMRecord_B *pRecord_B = NULL;
MainRecordHeader mainHeader;
IndexRecordHeader indexHeader;
// Calculate the array sizes
arraySize = pExt->imgWidth * pExt->imgHeight;
recordSize_A = sizeof (MultiPointMRecord_A) + arraySize * sizeof (Vertex);
recordSize_B = sizeof (MultiPointMRecord_B) + arraySize * sizeof (double);
// Allocate memory for the max size records
pRecord_A = calloc (1, recordSize_A);
pRecord_B = calloc (1, recordSize_B);
// Fill out the MultiPointMRecord_A
StoreShapeInteger (SE_MultiPointM, LittleEndian, &pRecord_A->ShapeType);
StoreShapeDouble (pExt->limWest, LittleEndian, &pRecord_A->Bounds.Xmin);
StoreShapeDouble (pExt->limSouth, LittleEndian, &pRecord_A->Bounds.Ymin);
StoreShapeDouble (pExt->limEast, LittleEndian, &pRecord_A->Bounds.Xmax);
StoreShapeDouble (pExt->limNorth, LittleEndian, &pRecord_A->Bounds.Ymax);
for (row = i = j = 0; row < pExt->imgHeight; row++)
{
for (col = 0; col < pExt->imgWidth; col++, i++)
{
if (pExt->extImage[row][col])
{
int status = SearchDataBase (NULL, pExt->limWest + col, pExt->limSouth, pExt->zone, &height);
StoreShapeDouble (pExt->limWest + col, LittleEndian, &pRecord_A->Parray[i].Xpoint);
StoreShapeDouble (pExt->limSouth + row, LittleEndian, &pRecord_A->Parray[i].Ypoint);
if (status < 0)
height = 0.0;
StoreShapeDouble ((double)height, LittleEndian, &pRecord_B->Marray[i]);
if (0 == j++)
{
minM = height;
maxM = height;
}
else
{
if (height < minM)
minM = height;
else if (height > maxM)
maxM = height;
}
}
}
}
StoreShapeInteger (arraySize, LittleEndian, &pRecord_A->NumPoints);
// Fill out the MultiPointMRecord_B
StoreShapeDouble ((double)minM, LittleEndian, &pRecord_B->Mmin);
StoreShapeDouble ((double)maxM, LittleEndian, &pRecord_B->Mmax);
// Create the shp Record Header
StoreShapeInteger (++shpRecordCount, BigEndian, &mainHeader.RecordNumber);
StoreShapeInteger ((recordSize_A + recordSize_B) / 2, BigEndian, &mainHeader.ContentLength);
// Create the shx Record Header
StoreShapeInteger (shpPos / 2, BigEndian, &indexHeader.RecordOffset);
StoreShapeInteger ((recordSize_A + recordSize_B) / 2, BigEndian, &indexHeader.ContentLength);
// Write the records to the shape files
fwrite (&mainHeader, sizeof (MainRecordHeader), 1, shpStream);
> fwrite (pRecord_A, recordSize_A, 1, shpStream);
fwrite (pRecord_B, recordSize_B, 1, shpStream);
shpSize += (sizeof (MainRecordHeader) + recordSize_A + recordSize_B) / 2;
fwrite (&indexHeader, sizeof (IndexRecordHeader), 1, shxStream);
shxSize += sizeof (IndexRecordHeader) / 2;
04-30-2008 02:06 PM
04-30-2008 04:18 PM - edited 04-30-2008 04:20 PM