Solved

What happens to memory ??

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
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.
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now