开发者

How is the memory of a class handled in C++? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

How are objects stored in memory in C++?

For example a C++ class:

class A{
 int value;
 void addOne(){
  value++;
 }
}

Will an instance of class A be loaded like this [pseudo code]:

[identifier of A]
[this is int value]
[this is void addOne(void)][value++]
Or like 开发者_如何学编程this:
[members identifier of A]
[this is int value]

[functions identifier of A] [this is void addOne(ref to member of A)][A.value++]

Second should use less memory on multiple instances of a class. Because the same functions are used for all instances. How is the memory handled in C++? Is it possible to change memory handling?


You are asking if the member functions are stored in the instances of the class? No. Each instance uses the same functions, with it's[the instance's] address passed as the hidden parameter this.


The layout is actually more like this:

Class instance:

[this is int value]

Somewhere else:

[this is void addOne(ref to member of A)][A.value++]

That is, a class consists of (exactly) its member variables and its base classes, nothing more (unless your class contains virtual functions, in which case it also contains a virtual function table) – in particular no “identifier”.

Same for its functions, which are stored somewhere else entirely, and not once for each class instance. Furthermore, a function of a class doesn’t contain a reference to its class either, nor to its members. It is just a memory block of code (machine code statements). When calling the function, you are (basically) *jumping to that location after pushing a pointer to the class instance onto the call stack. The method can then access the instance (and thus its members) by accessing the pointer on the stack.


If your question is about the memory layout of class A it is the same as having a struct A i.e. the integer value. The 4 bytes (or whatever bytes for int for that platform).
Functions are not part of the memory layout e.g. as pointers stored in function or something similar, so they do not affect the size of the class.
If the class A though was polymorphic class the size would be different as it would contain also the pointer to vtable.


Well, on Randall' Hyde's Write Great Code Volume 2 (great book, read it if you have some free time) there's a section that speaks just about that. Briefly, a class holds variables just like structs, but it has a record which keeps pointers to functions declared in that class (VMT):

VMT stands for virtual method table, and these 4 bytes contain a pointer to an array of “method pointers” for the class. Virtual methods (also known as virtual member functions in C++) are special class-related functions that you declare as fields in the class.
[...]
Calling a virtual member function requires two indirect accesses. First, the program has to fetch the VMT pointer from the class object and use that to indirectly fetch a particular virtual function address from the VMT. Then the program has to make an indirect call to the virtual member function via the pointer it retrieved from the VMT.
[...]
For a given class there is only one copy of the VMT in memory. This is a static object so all objects of a given class type share the same VMT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜