Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What happens to memory ??

Posted on 1998-06-18
5
Medium Priority
?
202 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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

610 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