468,457 Members | 1,640 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

sizeof struct

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ronald A. Andersen | last post: by
19 posts views Thread by Martin Pohlack | last post: by
10 posts views Thread by Mark A. Odell | last post: by
12 posts views Thread by news.fe.internet.bosch.com | last post: by
18 posts views Thread by Mockey Chen | last post: by
8 posts views Thread by Chameleon | last post: by
72 posts views Thread by goacross | last post: by
14 posts views Thread by ManicQin | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.