Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof struct

Reply
Thread Tools

sizeof struct

 
 
tom
Guest
Posts: n/a
 
      10-24-2006
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.


 
Reply With Quote
 
 
 
 
huangshan
Guest
Posts: n/a
 
      10-24-2006

"tom" <(E-Mail Removed)> wrote in message news:ehke3d$h9e$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
tom
Guest
Posts: n/a
 
      10-24-2006
I am using VC++ .NET

t.


 
Reply With Quote
 
huangshan
Guest
Posts: n/a
 
      10-24-2006
i see
because __int64 x2; // 8

so sizeof(w1)/8 must integer

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


 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      10-24-2006
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
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      10-24-2006

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.

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-24-2006

"tom" <(E-Mail Removed)> wrote in message news:ehkeu2$jq7$(E-Mail Removed)...
>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


 
Reply With Quote
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      10-25-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments