difference between struct and class

T

thomas

struct A{
int a;
char b;
};
class B{
int a;
char b;
};

sizeof(A)=6; sizeof(B)=8. why?
 
V

Victor Bazarov

thomas said:
struct A{
int a;
char b;
};
class B{
int a;
char b;
};

sizeof(A)=6; sizeof(B)=8. why?

Hm... This is probably covered in your compiler documentation. Try
reading it. The Standard does not dictate what the size should be.
It's "implementation-defined".

There is a difference between your types, you know that, yes?

V
 
T

thomas

thomas said:
struct A{
   int a;
   char b;
};
class B{
   int a;
   char b;
};
sizeof(A)=6;  sizeof(B)=8.    why?

Hm...  This is probably covered in your compiler documentation.  Try
reading it.  The Standard does not dictate what the size should be.
It's "implementation-defined".

There is a difference between your types, you know that, yes?

V


I don't know. I'm using vs2005.
I suppose "struct" and "class" should be equivalent except the default
privilege.
There's no reason to set different padding, if my understanding is
right.
 
F

Fred

Hm...  This is probably covered in your compiler documentation.  Try
reading it.  The Standard does not dictate what the size should be..
It's "implementation-defined".

There is a difference between your types, you know that, yes?

I don't know. I'm using vs2005.
I suppose "struct" and "class" should be equivalent except the default
privilege.
There's no reason to set different padding, if my understanding is
right.- Hide quoted text -

- Show quoted text -


Who has a 'this', and who doesn't?
 
V

Victor Bazarov

Fred said:
thomas wrote:
struct A{
int a;
char b;
};
class B{
int a;
char b;
};
sizeof(A)=6; sizeof(B)=8. why?
Hm... This is probably covered in your compiler documentation. Try
reading it. The Standard does not dictate what the size should be.
It's "implementation-defined".
There is a difference between your types, you know that, yes?
V

I don't know. I'm using vs2005.
I suppose "struct" and "class" should be equivalent except the default
privilege.
There's no reason to set different padding, if my understanding is
right.- Hide quoted text -

- Show quoted text -


Who has a 'this', and who doesn't?


Nobody "has a 'this'" since there are no non-static member functions.
'this' only exists in that context.

OTOH, IIUIC, the 'struct' is an aggregate and the 'class' isn't in your
case. So, they *can* have different alignment requirements, and those
*are* (again) compiler-specific. Please take a look in the
documentation that came with your compiler. There is also the newsgroup
for discussions on VC++, 'microsoft.public.vc.language'. You should
consider posting there as well.

V
 
T

thomas

Fred said:
thomas wrote:
struct A{
   int a;
   char b;
};
class B{
   int a;
   char b;
};
sizeof(A)=6;  sizeof(B)=8.    why?
Hm...  This is probably covered in your compiler documentation.  Try
reading it.  The Standard does not dictate what the size should be.
It's "implementation-defined".
There is a difference between your types, you know that, yes?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I don't know. I'm using vs2005.
I suppose "struct" and "class" should be equivalent except the default
privilege.
There's no reason to set different padding, if my understanding is
right.- Hide quoted text -
- Show quoted text -

Who has a 'this', and who doesn't?

Nobody "has a 'this'" since there are no non-static member functions.
'this' only exists in that context.

OTOH, IIUIC, the 'struct' is an aggregate and the 'class' isn't in your
case.  So, they *can* have different alignment requirements, and those
*are* (again) compiler-specific.  Please take a look in the
documentation that came with your compiler.  There is also the newsgroup
for discussions on VC++, 'microsoft.public.vc.language'.  You should
consider posting there as well.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -


Is "this" pointer an issue? The size of the following struct is 1,
which is the same as an empty struct. It confuses me when addressing
the "this" here. Why the function pointer in struct A is not counted
in the struct size is also not clear to me.

struct A{
public:
void f(){}
};
 
B

Bo Persson

thomas said:
Fred said:
On Nov 13, 11:50 pm, Victor Bazarov <[email protected]>
wrote:
thomas wrote:
struct A{
int a;
char b;
};
class B{
int a;
char b;
};
sizeof(A)=6; sizeof(B)=8. why?
Hm... This is probably covered in your compiler documentation.
Try reading it. The Standard does not dictate what the size
should be. It's "implementation-defined".
There is a difference between your types, you know that, yes?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I don't know. I'm using vs2005.
I suppose "struct" and "class" should be equivalent except the
default privilege.
There's no reason to set different padding, if my understanding
is right.- Hide quoted text -

- Show quoted text -
Who has a 'this', and who doesn't?

Nobody "has a 'this'" since there are no non-static member
functions. 'this' only exists in that context.

OTOH, IIUIC, the 'struct' is an aggregate and the 'class' isn't in
your case. So, they *can* have different alignment requirements,
and those *are* (again) compiler-specific. Please take a look in
the
documentation that came with your compiler. There is also the
newsgroup
for discussions on VC++, 'microsoft.public.vc.language'. You should
consider posting there as well.

V


Is "this" pointer an issue? The size of the following struct is 1,
which is the same as an empty struct. It confuses me when addressing
the "this" here. Why the function pointer in struct A is not counted
in the struct size is also not clear to me.

struct A{
public:
void f(){}
};


No, the 'this' pointer is not stored in the struct, it is rather the
address of the struct, known by the compiler.

Also, you struct doesn't contain a functions pointer, it is just a
function - which is code, not data.


Bo Persson
 
A

AnonMail2005

struct A{
   int a;
   char b;};

class B{
   int a;
   char b;

};

sizeof(A)=6;  sizeof(B)=8.    why?

For Visual C++ 2008 Express Edition, both of the above have sizeof 8.
Since sizeof(int) is 4, it looks like the struct/class is expanded for
alignment.
Adding another int to both gets you sizeof 12, all of which makes
sense.

Bottom line, for this compiler there is no difference between the
struct and class in terms of sizeof.

HTH
 
J

James Kanze

(Well, I can think of one big reason why they shouldn't be
different: If the struct really has a size of 6 bytes, you
will be getting memory alignment problems if you eg. use an
array of such structs. Accessing the 'a' member of each other
object in the array will cross a word boundary, which in many
processors will be inefficient because it causes penalty clock
cycles. And this assuming it doesn't cause a bus error, like
in some architectures; but I assume this is on an Intel
processor, so an error won't happen, but an inefficiency may
well happen.)

Since he didn't specify what platform he was on, you don't know
that making the size 6 will cause alignment problems:).

Victor suggested one ingenious reason why the sizes might be
different, and while he's totally correct in his analysis, I've
never heard of a compiler where this was the case. My bet is
that the code was compiled with different options, or some such,
because I've never heard of a compiler where the two would have
different sizes.
 
D

dragan

Fred said:
thomas wrote:
struct A{
int a;
char b;
};
class B{
int a;
char b;
};
sizeof(A)=6; sizeof(B)=8. why?
Hm... This is probably covered in your compiler documentation. Try
reading it. The Standard does not dictate what the size should
be. It's "implementation-defined".

There is a difference between your types, you know that, yes?

I don't know. I'm using vs2005.
I suppose "struct" and "class" should be equivalent except the
default privilege.
There's no reason to set different padding, if my understanding is
right.- Hide quoted text -

- Show quoted text -


Who has a 'this', and who doesn't?


You were maybe thinking about a class with virtual functions that has a
vptr? There is no "this" pointer injected by the compiler into a struct or
class, but if there was it would be larger than 2 bytes anyway.
 

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

Forum statistics

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

Latest Threads

Top