468,457 Members | 1,640 Online

sizeof struct

hiw is calculated size of struct
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
// int DataSize = sizeof(w1);
t.
Oct 24 '06 #1
7 3419

"tom" <ee@w.ewrote in message news:eh**********@nemesis.news.tpi.pl...
hiw is calculated size of struct
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
// int DataSize = sizeof(w1);
t.

i think 260 is right
Oct 24 '06 #2
I am using VC++ .NET

t.
Oct 24 '06 #3
i see
because __int64 x2; // 8

so sizeof(w1)/8 must integer

my englilsh poor,
do you know what i want to say?
Oct 24 '06 #4
tom wrote:
hiw is calculated size of struct
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
// int DataSize = sizeof(w1);
Google for member alignment. Most probably yours is set to 8 bytes, so
the x2 member will have an offset of 256 in your struct.

Regards,
Stuart
Oct 24 '06 #5

tom wrote:
hiw is calculated size of struct
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
// int DataSize = sizeof(w1);
t.
Padding, 260/8 = 32.5 and 264/8 = 34
for the same reason that a class like:

struct C
{
char c;
int n;
};

int main()
{
size_t size = sizeof(C);
std::cout << "size = " << size << std::endl;

return 0;
}

will also have a size matching the platform's memory indexing
architecture (thankfully).
Probably 8 but not neccessarily.
Regardless, it shouldn't matter in your case, unless you are
byte-copying the contents + padding around. Which you should not be
doing anyways.
Thats why operators and streams were created for.

Oct 24 '06 #6

"tom" <ee@w.ewrote in message news:eh**********@nemesis.news.tpi.pl...
>I am using VC++ .NET
That's nice. Is there some reason you're telling us all what compiler
you're using? Perhaps you're following up on a previous post? If so, then
you should be quoting the post you're responding to.

-Howard
Oct 24 '06 #7
tom wrote:
hiw is calculated size of struct
Size of members, plus size of padding. Padding is any memory used for
internal
compiler purposes. E.g. you'll often see a difference if a struct has
virtual functions,
but also if the members have mixed type.
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
Note that you have mixed types here.

My guess in this case is that __int64 is a compiler-defined type that
has
alignment requirements. If it must start at an address that's a
multiple of 8,
then struct w1 must also. In an array of w1's, each w1 should. This in
turn
means the sizeof(w1) must be a multiple of 8.

It may also be a speed optimization - again a 'internal compiler
purpose'.

HTH,
Michiel Salters

Oct 25 '06 #8