S****t 发帖数: 1186 | 1 I have a structure defined as:
typedef struct{
char FileCodes[4], U1[4], U2[4], U3[4], U4[4], U5[4], FileLength[4];
int Version, Shape;
double Xmin, Ymin, Xmax, Ymax, Zmin, Zmax, Mmin, Mmax;
}FileHeader;
I used "sizeof(FileHeader)" to get the size of this structure. When I use gcc
in Unix environment, I get 100, which is my expectation. However, when I use
VC in Windows XP, I get 104 all the time. I am really confused about this.
Any help? Thanks. | g*****g 发帖数: 34805 | 2 That's exactly why you need sizeof in C/C++ and not java,
An int could be 16 bits in some platform, 32 and 64 in another
after compiled by and on different compilers and platforms.
C++ specs didn't ask them to fix it, other expect them to
optimize it.
gcc
【在 S****t 的大作中提到】 : I have a structure defined as: : typedef struct{ : char FileCodes[4], U1[4], U2[4], U3[4], U4[4], U5[4], FileLength[4]; : int Version, Shape; : double Xmin, Ymin, Xmax, Ymax, Zmin, Zmax, Mmin, Mmax; : }FileHeader; : I used "sizeof(FileHeader)" to get the size of this structure. When I use gcc : in Unix environment, I get 100, which is my expectation. However, when I use : VC in Windows XP, I get 104 all the time. I am really confused about this. : Any help? Thanks.
| S****t 发帖数: 1186 | 3 right. I see what you mean. I checked the size of "char", "int" and "double"
for both platform (Unix and WinXP). Both give the same sizes, i.e., for char,
the size is 1, for int, the size is 4, for double, the size is 8. This
produces
100 for the total size in my case (7 * 4 * 1 + 2 * 4 + 8 * 8 = 100).
In Unix, I get this size. In WinXP, I get 104. I am really confused by
the extra 4 bytes.
【在 g*****g 的大作中提到】 : That's exactly why you need sizeof in C/C++ and not java, : An int could be 16 bits in some platform, 32 and 64 in another : after compiled by and on different compilers and platforms. : C++ specs didn't ask them to fix it, other expect them to : optimize it. : : gcc
| g*****g 发帖数: 34805 | 4 I think XP aligns for a dword which is 8 bytes, that's why you get
104. Reduce a char or two, you'll still get 104.
,
【在 S****t 的大作中提到】 : right. I see what you mean. I checked the size of "char", "int" and "double" : for both platform (Unix and WinXP). Both give the same sizes, i.e., for char, : the size is 1, for int, the size is 4, for double, the size is 8. This : produces : 100 for the total size in my case (7 * 4 * 1 + 2 * 4 + 8 * 8 = 100). : In Unix, I get this size. In WinXP, I get 104. I am really confused by : the extra 4 bytes.
| S****t 发帖数: 1186 | 5 thanks a lot! this makes sense. i have a further question.
since the file i am reading has a fixed header length of 100.
can i still use
fread(&shapeFileHeader, sizeof(shapeFileHeader), 1, fpShapeFile)
to read the header into the header structure or I need to read the fields (
like
Shape, Xmin, etc) one by one to address the disparity? Thanks again!
【在 g*****g 的大作中提到】 : I think XP aligns for a dword which is 8 bytes, that's why you get : 104. Reduce a char or two, you'll still get 104. : : ,
| t****t 发帖数: 6806 | 6 of course you can if sizeof() is 100;
otherwise you can't.
this is obvious...
【在 S****t 的大作中提到】 : thanks a lot! this makes sense. i have a further question. : since the file i am reading has a fixed header length of 100. : can i still use : fread(&shapeFileHeader, sizeof(shapeFileHeader), 1, fpShapeFile) : to read the header into the header structure or I need to read the fields ( : like : Shape, Xmin, etc) one by one to address the disparity? Thanks again!
| S****t 发帖数: 1186 | 7 just want to confirm :-) the alignment makes me really puzzled. thanks
【在 t****t 的大作中提到】 : of course you can if sizeof() is 100; : otherwise you can't. : this is obvious...
| t****t 发帖数: 6806 | 8 usually there are some compiler options to enable/disable
certain aspects of alignments. but if you want to use, be
very careful, since you must use it consistently over all
your programs. better way is to paddle manually so that
the result is the same no matter what compiler you use.
【在 S****t 的大作中提到】 : just want to confirm :-) the alignment makes me really puzzled. thanks
| g****c 发帖数: 299 | 9 sounds like fortran programming tip
【在 t****t 的大作中提到】 : usually there are some compiler options to enable/disable : certain aspects of alignments. but if you want to use, be : very careful, since you must use it consistently over all : your programs. better way is to paddle manually so that : the result is the same no matter what compiler you use.
| r*******q 发帖数: 50 | 10 #pragma pack(push,1)
your structure
#pragma pack(pop)
ALWAYS use above statements in your header file and you
will be happy all the time. ALWAYS manual patch structure size
to prefered size and use 1-byte-alignment will make your
program has less portable problem.
【在 t****t 的大作中提到】 : usually there are some compiler options to enable/disable : certain aspects of alignments. but if you want to use, be : very careful, since you must use it consistently over all : your programs. better way is to paddle manually so that : the result is the same no matter what compiler you use.
| t****t 发帖数: 6806 | 11 i don't think using pragma will make the program more
portable. although pragma pack is supported by quite
many compilers, pragma itself is always implementation-
dependent which means not portable. I would rather use
autoconf...
【在 r*******q 的大作中提到】 : #pragma pack(push,1) : your structure : #pragma pack(pop) : ALWAYS use above statements in your header file and you : will be happy all the time. ALWAYS manual patch structure size : to prefered size and use 1-byte-alignment will make your : program has less portable problem.
| r*******q 发帖数: 50 | 12 Your assumption is autoconf is more portable than pragma pack...
But it seems not true.
【在 t****t 的大作中提到】 : i don't think using pragma will make the program more : portable. although pragma pack is supported by quite : many compilers, pragma itself is always implementation- : dependent which means not portable. I would rather use : autoconf...
| t****t 发帖数: 6806 | 13 ok, autoconf is not necessary better than pragma pack.
but in reality, most gnu package use autoconf.:)
【在 r*******q 的大作中提到】 : Your assumption is autoconf is more portable than pragma pack... : But it seems not true.
| r*******q 发帖数: 50 | 14 That does not mean autoconf is good way to do that.
And, there is something autoconf can't do or is difficult to do
but pragma pack can do easily. And such condition is much more
important than portability. Indeed pragma pack's primary
purpose is NOT portability...
【在 t****t 的大作中提到】 : ok, autoconf is not necessary better than pragma pack. : but in reality, most gnu package use autoconf.:)
|
|