Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an object look like in memory? [duplicate]

Tags:

c++

Possible Duplicate:
Structure of a C++ Object in Memory Vs a Struct
memory layout c++ objects

This is probably a really dumb question, but I will ask anyway. I am curious what an object looks like in memory. Obviously it would have to have all of its member data in it. I assume that functions for an object would not be duplicated in memory (or maybe I am wrong?). It would seem wasteful to have 999 objects in memory all with the same function defined over and over. If there is only 1 function in memory for all 999 objects, then how does each function know who's member data to modify (I specifically want to know at the low level). Is there an object pointer that gets sent to the function behind the scenes? Perhaps it is different for every compiler?

Also, how does the static keyword affect this? With static member data, I would think that all 999 objects would use the exact same memory location for their static member data. Where does this get stored? Static functions I guess would also just be one place in memory, and would not have to interact with instantiated objects, which I think I understand.

like image 459
NeilMonday Avatar asked Sep 11 '12 21:09

NeilMonday


People also ask

What is an object in memory?

Programs obtain storage above the bar in chunks of virtual storage called memory objects . The system allocates a memory object as a number of virtual segments; each segment is a megabyte in size and begins on a megabyte boundary.

What is the memory structure of an object in C++?

There are two parts of memory in which an object can be stored: stack – Memory from the stack is used by all the members which are declared inside blocks/functions. Note that the main is also a function. heap – This memory is unused and can be used to dynamically allocate the memory at runtime.

Does class have memory in c++?

In general a class or struct is a concept and does not occupy variable (data) space, but it does take up memory in the compiler's memory.

Where are C++ objects allocated?

All dynamic allocations are on the heap.


1 Answers

Static class members are treated almost exactly like global variables / functions. Because they are not tied to an instance, there is nothing to discuss regarding memory layout.

Class member variables are duplicated for each instance as you can imagine, as each instance can have its own unique values for every member variable.

Class member functions only exist once in a code segment in memory. At a low level, they are just like normal global functions but they receive a pointer to this. With Visual Studio on x86, it's via ecx register using thiscall calling convention.

When talking about virtual functions, polymorphism, then the memory layout gets more complicated, introducing a "vtable" which is basically a bunch of function pointers that define the topography of the class instance.

like image 112
tenfour Avatar answered Oct 03 '22 23:10

tenfour