Skip to main content

25 posts tagged with "C++"

View All Tags

Memory Layout of a class (C++) Object

· One min read

Let us know how the members of a class are stored in the C++ class object.

  1. All Static Members Functions, Static member variables and Non static Members Functions are hoisted outside the class object.
  2. All non static member variables are stored in the class object.
  3. All virtual functions are part of Virtual Table. And a pointer (vptr) to the created Virtual Table is inserted with in each class object.

Sample Program:

class Sample
{
public:
Sample() {};

virtual ~Sample() {}
virtual void virtualFun1() {}
virtual void virtualFun2() {}

void normalFun() {}

static int getCount() //static function
{
return nCount;
}

private:
int i;
static int nCount;
};

void main()
{
Sample obj;
cout<<obj;
}

Size Matters (C++)

· 2 min read

C++ class have

A. Data Members B. Members Functions

Data Members

  1. Static Data Member The size of a class object with only Static data members irrespective of Data Type (say float, long e.tc.) is equal to one Byte (~ size of Empty class)
    class CStaticDataMemberCls
{
public:
static int i;
static float f;
};


void main() {
CStaticDataMemberCls objSDMC;
cout<<objSDMC;
}
  1. Non Static Data Member The size of a class with non static data members is equal to sum of the data type size i.e. size of int = 4 size of float = 4 so total = 8
    class CNonStaticDataMemberCls
{
public:
int i;
float f;
};


void main() {
CNonStaticDataMemberCls objNSDMC;
cout<<objNSDMC;
}

Members Functions

  1. Static Members Functions
  2. Non Static Members Functions

As Static Members Functions and Non static Members Functions are hoisted outside the class object. The size of the class will also be equal to one Byte (~ size of Empty class)

class CMemberFunctionsCls
{
public:
int fun1() { return 1; }
static int staticFun() { return 1; }

};

void main()
{
CMemberFunctionsCls objMFC;
cout<<objMFC;
}
  1. Virtual Members Functions If a class consists of virtual functions a table of pointers(i.e. Virtual Table) to virtual functions is generated for each class. And a pointer (vptr) to the created Virtual Table is inserted with in each class object. So the size of CVirtualFunctionCls object will be 4 Bytes which is nothing but a size of vptr.
    class CVirtualFunctionCls
{
public:
virtual ~CVirtualFunctionCls() {}
virtual void virtualFun1() {}
virtual void virtualFun2() {}
};


void main() {
CVirtualFunctionCls objVFC;
cout<<objVFC;
}

Let us see the class with all the above members

class Sample
{
public:
Sample() {};

virtual ~Sample() {}
virtual void virtualFun1() {}
virtual void virtualFun2() {}

void normalFun() {}

static int getCount() //static function
{
return nCount;
}

private:
int i;
static int nCount;
};

void main()
{
Sample obj;
cout<<obj;
}

Variable arguments handling in C/C++

· One min read

Use va_list to accept a VARYING NUMBER OF ARGUMENTS for any function in C/C++. printf(const char*_Format, ...) is a real time function which uses va_list.

For using va_list we need to know about the following macros

va_start Initialize a variable argument list (macro) va_arg Retrieve next argument (macro) va_end End using variable argument list (macro)

The sample explains how to use VARYING NUMBER OF ARGUMENTS


# include <stdarg.h>

int Add(int args, ...)
{
int sum = 0;
int temp = 0;

va_list va; //1. Declare a va_list

va_start(va, args); //2. Initialise

for(int i = 0; i<=args; i++) {
temp = va_arg(va, int); //3. Retrieve
sum = temp+sum;
}

va_end(va); //4. END

return sum;
}

void main() {
printf("sum=%d n ", Add(2, 1, 2, 5));
//OutPut: 8
}

Why size of an empty C++ class not zero?

· One min read

To ensure that the addresses of two different objects will be different C++ compiler will allocate one byte of memory. The below sample code explains clearly. Because of this behaviour the addresses of class X objects are not equal even though there is no data present in the class X.

class X {

};

class Y {

};

void main() {
X objX1, objX2; Y objY;

cout<<"size of objX1: "<<sizeof(objX1)<<endl; //Output: 1
cout<<"size of objY: "<<sizeof(objY)<<endl; //Output: 1

//Addresses
cout<<"Address of objX1: "<<&objX1<<endl;
cout<<"Address of objX2: "<<&objX2<<endl;

cout<<"Address of objY: "<<&objY<<endl;

if(&objX1 == &objX2) {
cout<<"Never Happens";
}
}

C FAQ

· One min read
  1. How to write a code to count number of characters/Spaces/numbers/special characters present in a sentence.  sentence ="1. GOD IS GREAT !!!"

2. How to insert space between each character in a sentence. sentence ="GOOD MORNING" newsentence = "G O O D  M O R N I N G"

Click here to read complete Article

Tags: