?
Solved

What happens to memory ??

Posted on 1998-06-18
5
Medium Priority
?
198 Views
Last Modified: 2010-04-01
If I have  a class that is instantiated locally, and now the object's ctor allocates memory for its purpose. Does the object itself lives in the heap and so is its allocated data members ? or The object is allocated to the function's stack and the objects allocated data members live on the heap ?

I would like to know the difference scenarios of what data goes where.
0
Comment
Question by:migue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:kellyjj
ID: 1166274
I believe that the object should not exist outside the scope of where you init it.  However, if the obj is not needed globally, then you should clean it up before leaving.
0
 
LVL 3

Accepted Solution

by:
gaohong earned 120 total points
ID: 1166275
If it is newed on heap, then it stays there until you delete it.
To put it on stack, do not use newed one
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166276
If the object is instanciated locally, that is, it is not created using the new operator, then the object is allocated on the stack not on the heap.  However if the object allocates memory with new inside its ctor (or other procedures), that memory is on the heap.  However the object itself remains on the stack.  It just uses a pointer to memory in the heap.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166277
There are 3 locations for data.  

Global data that is data that is not defined inside (local to)  a procedure and is not allocated with new, is stored in the program's data areas.  Often room is allocated for this data alongside the program's code.

Local data is data that is declared inside a procedure without using the new operator.  It is allcoated on the program's stack.

Dynamic data is allocated using the new operator (or C's malloc).  It is allocated on the program's heap (or heaps).

0
 
LVL 2

Expert Comment

by:VEngineer
ID: 1166278
let's say you make a character array:

void f() {
   char stackBuffer[80];
   char* heapBuffer = new char[80];
}

stackBuffer array is stored on the stack, is destroyed automatically as you leave the function

the heapBuffer *pointer* is on the stack, is destroyed automatically as you leave the function

the *object* (the array of chars) is created on the heap and will stay there until you use delete to deallocate it

so if you don't delete the object that heapBuffer pointer is pointing to, you will lose the pointer and the access when you leave the function, but the object will still reside in memory, inaccessible (unless you use an object in a larger scope, like a class data member for instance).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question