471,089 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

sizeof class ?

Hi,
I have a class.
class C1
{
int i ;
char c ;
} ;

let me say the size of int is 4 and char is 1. Then if I say sizeof(C1)
it should be 5, why is it showing 8 ?

Apr 25 '06 #1
9 8243
Sameer wrote:
Hi,
I have a class.
class C1
{
int i ;
char c ;
} ;

let me say the size of int is 4 and char is 1. Then if I say sizeof(C1)
it should be 5, why is it showing 8 ?

Because your compiler aligns data on 4 byte boundaries, which is typical
on a 32 bit system.

--
Ian Collins.
Apr 25 '06 #2
so concise! Right !

Apr 25 '06 #3
void wrote:
so concise! Right !

? please quite some context in your reply.

--
Ian Collins.
Apr 25 '06 #4
So how can I correct this? Is it only possible that the size of a class
is only multiple of size of int ?

Apr 27 '06 #5
"Sameer" <en******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
So how can I correct this? Is it only possible that the size of a class
is only multiple of size of int ?


What do you mean by correcting it? It is correct. What is it about the
size that you don't like?

It may be possible to compact the class/structure by a compiler specific
switch, but this can cause problems. There are some systems that an
unaligned value can cause the CPU to not read it correctly. It can also
slow down operation.

I would suggest you just leave it alone and don't worry about the 3 extra
bytes.

If you really must compact it for some reason, look at your compilers
switches and such. If you're using a microsoft compiler look at #pragma
pack
Apr 27 '06 #6
That question (How to make the size fo class exactly as the total size
of variables defined?) was asked with me in 2 interviews. Thats why i
asked.

May 17 '06 #7
first, what is the size of an empty class? what the empty class
contains?

May 17 '06 #8
sk*******@yahoo.co.in wrote:
first, what is the size of an empty class?
Anything bigger than zero.
what the empty class contains?


Padding bytes that ensure that the size is not zero.

May 17 '06 #9
Rolf Magnus <ra******@t-online.de> wrote:
sk*******@yahoo.co.in wrote:
first, what is the size of an empty class?


Anything bigger than zero.


....as long as it is not an empty base class for a derived class. In
that case, it can be zero. However, on its own, I agree it must be
nonzero, so that distinct objects can be distinguished.
#include <iostream>

class Base { };

class Derived : public Base {
int x;
};

class OneInt {
int x;
};

int main()
{
std::cout << "sizeof int: " << sizeof(int) << '\n';
std::cout << "sizeof Base: " << sizeof Base << '\n';
std::cout << "sizeof Derived: " << sizeof Derived << '\n';
std::cout << "sizeof OneInt: " << sizeof OneInt << '\n';
}
/*
Output on my machine:
sizeof int: 4
sizeof Base: 1
sizeof Derived: 4
sizeof OneInt: 4
*/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sunil Menon | last post: by
3 posts views Thread by yuyang08 | last post: by
sanjay123456
8 posts views Thread by sanjay123456 | last post: by
6 posts views Thread by deepakvsoni | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.