Dive Deep into CPP : sizeof() operator

sizeof() operator works on the type of the object.So sizeof() operator never reaches runtime. It is computed during compile time itself.

class a{};
int main(){
cout<<sizeof(a); // This will print “1” as output.
return 0;
}

Null object is an object to a class with no members. To preserve the sanity of the class and object, by default all the cpp compilers will allocate one byte to the empty class. Because there is no point in allocating and deallocating a class with no memory.If we compile sizeof() operator with the empty class object also we will get 1byte as output (because as mentioned earlier it operates on the type of the object).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s