a c/c++ problem ???

I am write a IM filter . but have a c/c++ problem ?

struct m
{
int m_int1;
int m_int2;

short m_short1;
};

int main(int argc, char* argv)
{

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

m a;

printf(“sizeof(m) = %d\n”,sizeof(a)); //??? but length = 12
return 0;
}

sizeof(a) = 12 why not is sizeof(int)+sizeof(int)+sizeof(short) == 10 ???

if a struct have no member , it’s value’s length is 1 . but why not is 0 ???

thank you .

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
?
-----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 ?
?
struct m
{
?int m_int1;
?int?m_int2;
?
?short m_short1;
};
?
int main(int argc, char* argv)
{
?
?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
?
?m??a;
?
?printf(“sizeof(m) = %d\n”,sizeof(a));??? ??? //??? but length = 12
?return 0;???
}
?
sizeof(a) = 12?? why not is sizeof(int)+sizeof(int)+sizeof(short) == 10 ???
?
?
if a struct have no member , it’s value’s length is 1 . but why not is 0
???
?
?
?
?thank you .

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;
?
?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.

:slight_smile:

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
?
-----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 ?
?
struct m
{
?int m_int1;
?int?m_int2;
?
?short m_short1;
};
?
int main(int argc, char* argv)
{
?
?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
?
?m??a;
?
?printf(“sizeof(m) = %d\n”,sizeof(a));??? ??? //??? but length = 12
?return 0;???
}
?
sizeof(a) = 12?? why not is sizeof(int)+sizeof(int)+sizeof(short) == 10 ???
?
?
if a struct have no member , it’s value’s length is 1 . but why not is 0
???
?
?
?
?thank you .


You are currently subscribed to ntfsd as: xxxxx@appstream.com
To unsubscribe send a blank email to %%email.unsub%%

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.

:slight_smile:

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

> If one defines
>
> #pragma pack(push, 1)
> struct m
> {
> int m_int1;
> int m_int2;
>
> short m_short1;
> };
> #pragma pack(pop)
>
> the sizeof(struct m) is 12.

  1. At packing level 1, no padding is provided by the compiler.

Cheers,
Felix.

The compiler pads structures to be aligned for speed (cache issues).
Use #pragma pack(1) around the structure declaration to avoid this.

Max

----- Original Message -----
From: wangyp
To: File Systems Developers
Sent: Wednesday, November 20, 2002 5:15 AM
Subject: [ntfsd] a c/c++ problem ???

I am write a IM filter . but have a c/c++ problem ?

struct m
{
int m_int1;
int m_int2;

short m_short1;
};

int main(int argc, char* argv)
{

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

m a;

printf(“sizeof(m) = %d\n”,sizeof(a)); //??? but length = 12
return 0;
}

sizeof(a) = 12 why not is sizeof(int)+sizeof(int)+sizeof(short) == 10 ???

if a struct have no member , it’s value’s length is 1 . but why not is 0 ???

thank you .
b???.???????v???jɚ???ځ?+i??d??{.n???zwZnV??隊[h???z{_?ݴ?p%??l??

As Tony said, this is a feature. Among other things, the padding insures that if you have an array of structures, the alignment of each structure in the array is optimal (or correct depending on your processor family – some processors require data to be aligned.)

-----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 ?

struct m
{
int m_int1;
int m_int2;

short m_short1;
};

int main(int argc, char* argv)
{

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

m a;

printf(“sizeof(m) = %d\n”,sizeof(a)); //??? but length = 12
return 0;
}

sizeof(a) = 12 why not is sizeof(int)+sizeof(int)+sizeof(short) == 10 ???

if a struct have no member , it’s value’s length is 1 . but why not is 0 ???

thank you .
b???.???????v???j?hl[???ܢd??{.n???zwZnV??隊[h???z{_???e??l??

> if a struct have no member , it’s value’s length is 1 . but why not is 0 ???

According to C++ standards all objects must be allocated unique memory locations whether on the stack or on the heap. So if using the new operator for creating two objects of the same stucture, a unique address can be generated for each call only if there is at least 1 byte of memory to allocate.

Santosh