RE: [ntfsd] RE: a c/c++ problem ???Thank you .
----- Original Message -----
From: Bi Chen
To: File Systems Developers
Sent: Wednesday, November 20, 2002 11:02 AM
Subject: [ntfsd] RE: a c/c++ problem ???
The guy (Wang) is apparently a rooky to C/C++ language.
Compiler packs intrinsic members of structure based on the rule that if the pack size is larger than a member size, the member is packed on the boundary of the member size, otherwise, the member is packed on the boundary of pack size. Default pack size is 8byte in most compiler for 32-bit CPU. The whole structure size is the pad to the CPU register size if the pack size is larger, otherwise, the pack size.
Why compiler is doing this? This is because (CPU) cache access of intrinsic data does not incur penalty if the data is aligned to the boundary of its size.
If one defines
#pragma pack(push, 1)
struct m
{
?int m_int1;
?int?m_int2;
?/FONT>
?short m_short1;
};
#pragma pack(pop)
the sizeof(struct m) is 12.
Try to rearrange order of the members of the following struct with default pack size 8-byte, you will find size
varies.
struct S
{
double d;
long l;
short s;
char c;
};
From the compiler’s pack rule, it is not difficult to find the above struct S which members are defined in the desending order of their size is of best practice, since it has the least size while not incur cache access penalty if the instance of the struct itself is aligned to the lagest of its intrinsic data member.

Bi
-----Original Message-----
From: Tony Mason [mailto:xxxxx@osr.com]
Sent: Tuesday, November 19, 2002 6:25 PM
To: File Systems Developers
Subject: [ntfsd] RE: a c/c++ problem ???
The compiler always pads out to the nearest boundary. Since your structure
contains DWORD values, the nearest boundary is a DWORD boundary. This is a
feature.
Regards,
Tony
Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com
?/FONT>
-----Original Message-----
From: wangyp [mailto:askmicrosoft@163.com]
Sent: Tuesday, November 19, 2002 9:16 PM
To: File Systems Developers
Subject: [ntfsd] a c/c++ problem ???
I am write a IM filter . but have a c/c++ problem ?
?/FONT>
struct m
{
?int m_int1;
?int?m_int2;
?/FONT>
?short m_short1;
};
?/FONT>
int main(int argc, char* argv)
{
?/FONT>
?printf(“sizeof(int) = %d\n”,sizeof(int));???// length?= 4
?printf(“sizeof(short) = %d\n”,sizeof(short));???// length =2
?printf(“sizeof(unsigned int) = %d\n”,sizeof(unsigned int));???//length =4
?printf(“sizeof(unsigned short) = %d\n”,sizeof(unsigned short));???//length
=2
?printf(“sizeof(char) = %d\n”,sizeof(char));???//length
=1
?/FONT>
?m??a;
?/FONT>
?printf(“sizeof(m) = %d\n”,sizeof(a));???//??? but length = 12
?return 0;???
}
?/FONT>
sizeof(a) = 12?? why not is sizeof(int)+sizeof(int)+sizeof(short) == 10 ???
?/FONT>
?/FONT>
if a struct have no member , it’s value’s length is 1 . but why not is 0
???
?/FONT>
?/FONT>
?/FONT>
?thank you .
You are currently subscribed to ntfsd as: xxxxx@appstream.com
To unsubscribe send a blank email to %%email.unsub%%
You are currently subscribed to ntfsd as: askmicrosoft@163.com
To unsubscribe send a blank email to xxxxx@lists.osr.com