Solved

What happens to memory ??

Posted on 1998-06-18
5
183 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
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 30 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

786 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