Solved

memory allocated to objects

Posted on 2004-10-13
5
174 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

12 Experts available now in Live!

Get 1:1 Help Now