由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - size of structure
相关主题
ask a question about struct in C programmingvc 2008: compilation error about header file
fread/fwrite有big/small endian问题吗?A helloworld OpenMP question?
有没有现成的模拟fread的bufferRead()?c++,这种做法不行?
serialization 到底该怎么理解啊?[合集] 请教一个calloc的问题
有偿 求 configure (Autoconf) scriptC++ interdependence question
关于Makefile的一个问题ask a simple question about int pointer.
关于C++ STL编译的疑问数组问题
ask a question about compile shared library using libtool怎么得到char *分配空间的大小?
相关话题的讨论汇总
话题: size话题: pragma话题: structure话题: use话题: autoconf
进入Programming版参与讨论
1 (共1页)
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.:)

1 (共1页)
进入Programming版参与讨论
相关主题
怎么得到char *分配空间的大小?有偿 求 configure (Autoconf) script
问个virtual table 的问题关于Makefile的一个问题
A question about class size关于C++ STL编译的疑问
C++ template preprocessorask a question about compile shared library using libtool
ask a question about struct in C programmingvc 2008: compilation error about header file
fread/fwrite有big/small endian问题吗?A helloworld OpenMP question?
有没有现成的模拟fread的bufferRead()?c++,这种做法不行?
serialization 到底该怎么理解啊?[合集] 请教一个calloc的问题
相关话题的讨论汇总
话题: size话题: pragma话题: structure话题: use话题: autoconf