sizeof struct

T

tom

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.
 
H

huangshan

tom said:
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
 
H

huangshan

i see
because __int64 x2; // 8

so sizeof(w1)/8 must integer

my englilsh poor,
do you know what i want to say?
 
S

Stuart Redmann

tom said:
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
 
S

Salt_Peter

tom said:
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.
 
H

Howard

tom said:
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
 
M

Michiel.Salters

tom said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top