avatar
S*t
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.
avatar
g*g
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.

avatar
S*t
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

avatar
g*g
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.

avatar
S*t
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.
:
: ,

avatar
t*t
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!

avatar
S*t
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...

avatar
t*t
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
avatar
g*c
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.

avatar
r*q
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.

avatar
t*t
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.

avatar
r*q
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...

avatar
t*t
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.

avatar
r*q
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.:)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。