Solved

memory allocated to objects

Posted on 2004-10-13
5
176 Views
Last Modified: 2010-04-01
hi ,
i heard that if we declare an object like
abc obj;
{assuming abc is a class}
it is allocated stack memory

and when
abc *obj=new abc
then it is allocated heap memory.
if it is true,why it is so.
thanks...
0
Comment
Question by:decentswati
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
aviadbd earned 100 total points
ID: 12305469

When you allocate memory using the "new" operator, your operating system searches for a free memory in the virtual memory the application get when it is started.  The benefit of the heap is that it is not released until you specificly delete it with the "delete" operator.

The stack, however, is more connected to the scope the object being allocated. So, it is automaticly deleted when you leave that scope.

Further, heap is usually allocated at run-time while stack is allocated (more or less) in compile-time.

Try reading more here:
http://www-ee.eng.hawaii.edu/Courses/EE150/Book/chap14/subsection2.1.1.8.html

AviadBD.
 
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 25 total points
ID: 12305806
>>i heard that if we declare an object like
>>abc obj;
>>{assuming abc is a class}
>>it is allocated stack memory

Not entirely true, only true if the line "abj obj;" is inside a function ,

in case the object obj is global , its is allocated neither in heap or stack.

check "overview of c++ memory management" http://www.cse.wustl.edu/~cdgill/courses/cs342/c++_memory_management.pdf
0
 
LVL 3

Expert Comment

by:aviadbd
ID: 12306004

True, avizit.

Also, if there is a member declaration inside a class, it will not be allocated at all until the class itself is allocated.

AviadBD.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12306089
Hi aviadbd,
> Also, if there is a member declaration inside a class, it will not be
> allocated at all until the class itself is allocated.

...unless it's static. Then it's "somehow" allocated at program start, with no guaranteed order.

Cheers!

Stefan
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

777 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