Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof class ?

Reply
Thread Tools

sizeof class ?

 
 
Sameer
Guest
Posts: n/a
 
      04-25-2006
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 ?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-25-2006
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.
 
Reply With Quote
 
 
 
 
void
Guest
Posts: n/a
 
      04-25-2006
so concise! Right !

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-25-2006
void wrote:
> so concise! Right !
>

? please quite some context in your reply.

--
Ian Collins.
 
Reply With Quote
 
Sameer
Guest
Posts: n/a
 
      04-27-2006
So how can I correct this? Is it only possible that the size of a class
is only multiple of size of int ?

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-27-2006
"Sameer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Sameer
Guest
Posts: n/a
 
      05-17-2006
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.

 
Reply With Quote
 
skishorev@yahoo.co.in
Guest
Posts: n/a
 
      05-17-2006
first, what is the size of an empty class? what the empty class
contains?

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      05-17-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      05-17-2006
Rolf Magnus <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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
 
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
sizeof of an instance differ from sizeof of a class GRenard C++ 2 11-03-2006 07:21 AM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(Class)/sizeof(Object) Nikolai Weibull Ruby 2 12-31-2004 06:42 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments