Do static members of a class occupy memory if no object of that class is created?

No.

Static members don’t belong to the instances of class. They don’t increase instances and class size even by 1 bit!

struct A
{
    int i;
    static int j;
};
struct B
{
    int i;
};
std::cout << (sizeof(A) == sizeof(B)) << std::endl;

Output:

1

That is, size of A and B is exactly the same. Static members are more like global objects accessed through A::j.

See demonstration at ideone : http://www.ideone.com/YeYxe


$9.4.2/1 from the C++ Standard (2003),

A static data member is not part of
the subobjects of a class. There is
only one copy of a static data member
shared by all the objects of the
class.

$9.4.2/3 and 7 from the Standard,

once the static data member has been
defined, it exists even if no objects
of its class have been created.

Static data members are initialized
and destroyed exactly like non-local
objects (3.6.2, 3.6.3)
.

As I said, static members are more like global objects!

Leave a Comment