Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ Dynamic Memory Allocation

Status
Not open for further replies.

damonh78

Programmer
Jul 28, 2005
44
IE
Hi I was wondering would someone have the answers to these questions or/and know where I could find a decent resource that would explain the area properly.
When i dynamically create an object of a certain class, memory is allocated for it on the heap, that bit I know, what I am trying to find out is firstly what is contained in this block of memory? Are the member variable stored there, am trying to find out are the objects functions stored there or as i think is the case, as many objects of the same type can be created, are the functions themselves stored in one place and each individual object has a pointer to them, if so where are they stored and are the local variables of these methods just stored on the stack when they are in scope.

Any help would be greatly appreciated as I am a complete newbie to this area and can only find basic information on the net which doesnt explain the above.

Thanks in Advance,

John
 
If dynamically create means by intrinsic (global) operator new[/b], then memory slot for the object allocated on the heap. Yes, this class member variables of the new object (except static members) stored there, plus (may be) some sort of control info (if the class has virtual functions or RTTI support is on). But functions codes never stored on the heap, it's (read only) part of your module compiled and lonked code. If the class has no virtual functions, no need to save any member functions pointers in data area.

As usually, local (automatic) variables of class member functions allocated in stack frames when methods invoked.

If a class variable declared as automatic, its memory slot allocated on the stack too.

If a class variable declared as static (global or local), its memory slot allocated implicitly before main() function started (by prologue code).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top