473,396 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 8420
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
4
by: Gopal-M | last post by:
I have the problem with sizeof operator I also want to implement a function that can return size of an object. My problem is as follows.. I have a Base class, say Base and there are many class...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
1
by: Bob | last post by:
Try the following code class basic { public: virtual void one()=0; }; class derived1:public basic { public:
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
3
by: yuyang08 | last post by:
Hi, I have #include <iostream> using namespace std; class A{ public: unsigned x : 5; unsigned y : 3;
8
sanjay123456
by: sanjay123456 | last post by:
dear Friends see the following code public class hello { public static void main(args) {
6
by: deepakvsoni | last post by:
what is the size of an class with not data members? ex: class A { }; also sizeof ? class A{ int f(){ cout<<"Hello";
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.