3

I am currently reading Inside the C++ Object Model. On page 9 it has a diagram showing how the contents of a class are laid out in memory. It states the only part of an object which actually resides in the class memory are non-static data members.

Here is a post from SO regarding the contents of memory for a program:

Global memory management in C++ in stack or heap?

In the second answer it details the memory layout of a program- showing the stack and the heap.

Does the location of the static data members/any class function (basically the parts of the class which are not stored within the object- referring to page 9) change depending whether the object is on the stack or the heap?

trincot
  • 317,000
  • 35
  • 244
  • 286
user997112
  • 29,025
  • 43
  • 182
  • 361

2 Answers2

4

Static data members reside in the same area of memory that global variables and plain static variables would reside. It is the "class memory" that could either be on the stack or heap, depending on how the instance of the class was created.

A static data member is not too different from a global variable. However, it is scoped by the class name, and its access by name can be controlled via public, private, and protected. public gives access to everyone. private would restrict access to only members of the class, and protected is like private but extends access to a class that inherits from the class with the static data member.

In contrast, a global variable is accessible by name by everyone. A plain static variable is accessible by name by code in the same source file.

A plain class method is actually just a regular function (modulo access controls), but it has an implicit this parameter. They do not occupy any space in a class. However, a virtual class method would occupy some memory in the class, since it has to resolve to a derived class's implementation of the method. But, polymorphism is likely not yet covered where you are in your textbook.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • Just to confirm, page 9 of said book states that a non-static function is also stored separately from the class object? I can understand why static stuff is (as you only have one copy of them, hence static) but I was surprised to read non-static functions weren't included in the object. – user997112 Jul 01 '12 at 16:59
  • @user997112: Class methods are like static methods, except the `this` parameter is implicit, while static methods have no `this` parameter (since the method is supposed to apply to any instance). – jxh Jul 01 '12 at 17:02
  • Thanks- I think I just answered my own question in my comment to the other poster. Its only the non-static data members which must be specific per object because they form the "state" of the object and can vary per object. In contrast, a class function is the same for whatever instance of that class? – user997112 Jul 01 '12 at 17:05
  • @user997112: Yes, except for `virtual`, as I stated in my edit. Regards – jxh Jul 01 '12 at 17:06
2

No, where variables are allocated doesn't affect the storage of static data or code. These are generally stored in separate memory areas, that are neither stack or heap.

Functions and static data members are special in that that there is only one copy of each in the whole program.

Variables of class, or other, types are most often created and destroyed multiple times during a program run.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • So basically, because non-static data members contribute to the "state" of an individual object- they are the only things to be included within the object address space? They have to be unique because each object could have different data member values. In contrast a non-static function is still the same function no matter which non-static object accesses it? – user997112 Jul 01 '12 at 17:03
  • Yes, the non-static data is specific to each object, so there could be many copies. The static data is shared, so there is only one copy of that. A member function only differs from an "ordinary" function in that it has a `this` pointer showing which object it was called for. Static member functions don't have `this`, so they are *very* similar to free functions (except they can be private, to limit access). – Bo Persson Jul 01 '12 at 17:12