UrbanPro
true

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Do You Know Size Of Empty Class and Reason?

Umasankar Natarajan
07/07/2017 0 0

Size of empty class is always 1 byte.

Reason is, in order to differentiate one object to another object, the size of empty class is always 1 byte.

See the below c++ code snippet. Here there are three object created, each object size is 1. Their address is +1 byte for each object.

#include
using namespace std;

// Empty class declaration
class Student
{
};

int main() {
    // Object creation
    Student s1,s2,s3;

    // Getting sizeof the object
    cout << "Size of empty class Student for object s1 " << sizeof(s1) << endl;
    cout << "Size of empty class Student for object s2 " << sizeof(s2) << endl;
    cout << "Size of empty class Student for object s3 " << sizeof(s3) << endl;

    // Getting address of the object
    cout << "Address of object s1 " << &s1 << endl;
    cout << "Address of object s2 " << &s2 << endl;
    cout << "Address of object s3 " << &s3 << endl;
    
    return 0;
}

output
Size of empty class Student for object s1 1
Size of empty class Student for object s2 1
Size of empty class Student for object s3 1

Address of object s1 0x7ffc4fb8572d
Address of object s2 0x7ffc4fb8572e
Address of object s3 0x7ffc4fb8572f

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

My first C++ program
# include using namespace std; int main() { cout<<"Welcome to C++"; return 0; } Use IDE such as CodeBlocks to run this program. To do this, open CodeBlocks, Goto File->New->Empty File. Copy...

Juhi G. | 09/07/2021

1 0
0

Magic in C=> OFFSET OF MACRO in C [offsetof(TYPE, MEMBER)]
Here I am going to discuss an exciting macro in C. It will give you offset of a member within a structure. NULL or 0 address is typecasted to TYPE first. It won't result in segmentation fault as we are...

Jyotishkar D. | 20/05/2019

1 0
0

Creating a class in c++
Introduction Before we proceed with understanding as to how to create a class in c++, we need to first understand the what is a class. Definition : A class is a container of related data and methods...
M

Mahesh C | 31/10/2018

0 0
0

Predict the Output of C Questions based on Recursion and Macro
#include<stdio.h>void k(unsigned int i,unsigned int j){ static char x="0123456789ABCDEF"; if(i>=j) k(i/j,j); putchar(x);}void main(){ k(20,12);} ___________________________________________ ...

Yashvardhan Soni | 19/10/2018

2 0
0

Advantages of C++ Language
Advantages of C++ - C++ is a profoundly convenient dialect and is frequently the dialect of decision for multi-gadget, multi-stage application advancement. - C++ is a protest situated programming dialect...

Looking for C++ Language Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
Sponsored
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

Book a Free Demo

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more