473,756 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8444
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.goo glegroups.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
3555
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 know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
4
3107
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 derived from it. At a particular point in my application I need the size of the object pointed to by the base pointer. Eg
2
2469
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
1243
by: Bob | last post by:
Try the following code class basic { public: virtual void one()=0; }; class derived1:public basic { public:
6
2471
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 type of its own class. Non-static data members can not be declared as type of its own class (excluding pointers and references).
3
1200
by: yuyang08 | last post by:
Hi, I have #include <iostream> using namespace std; class A{ public: unsigned x : 5; unsigned y : 3;
8
2380
sanjay123456
by: sanjay123456 | last post by:
dear Friends see the following code public class hello { public static void main(args) {
6
2953
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
1971
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. The original post at gnu.gcc.help can be found at this link http://groups.google.com/group/gnu.gcc.help/browse_thread/thread/ece55cbbd9c36270/2148a6c1ac6119e1?lnk=st&q=#2148a6c1ac6119e1 Here's the question: class base {
0
9287
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10046
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.