Say I have a class and I have a static member in it, but I don't create any objects of that type. Will the memory be occupied for the static variable? If it would be occupied, what is the point of putting it in a class?
3 Answers
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!

- 3,482
- 3
- 38
- 50

- 353,942
- 115
- 666
- 851
-
2+1 for linking to definitions from the standard, basically outlines the answer in concrete. – hiddensunset4 Jan 30 '11 at 09:22
-
YeYxe link is broken – Patrizio Bertoni Apr 27 '18 at 14:13
-
I submit that *"No."* is the wrong answer to the title question as it currently appears. By your own quotes from the standard, memory is set aside for static members regardless or the instantiation or lack there of of objects of the type; it's just stored "in" individual objects. – dmckee --- ex-moderator kitten Dec 13 '18 at 21:37
The C++ standard doesn't explicitly state when static memory is allocated, as long as it is available on first use. That said, it is most likely allocated during program initialization, thus guaranteeing its presence as soon as it is required, without needing special-case code to detect and perform allocation on access.
The purpose of putting static data into a class is the same as putting any other data into classes. By putting the data into a class structure, you are defining an encapsulating namespace, as well as being able to control access using accessor and mutator methods; this, in turn, will allow you to validate data going into the static memory store, and to ensure consistency throughout the use of this data.

- 5,057
- 3
- 25
- 30
-
Actually, I think the standard treats static members like all globals, and their instanciations is submitted, as far as I understand, to the same law that the instanciations of other globals. That is the initialization order is required to be consistent within a translation unit and there is nothing said about the multiplexing with other translation units. – Matthieu M. Jan 30 '11 at 14:22
Static variables are stored in a special memory area called BSS, while instances of a class are stored in a heap or on a stack. So, static members are stored separately.

- 364
- 2
- 3
-
9Actually, the existence of BSS is system-dependent. The C++ standard doesn't define any such beast, nor any details of the implementation of data allocation. – Kevin Lacquement Jan 30 '11 at 09:13