[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

memory allocated to objects

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
decentswati
Asked:
decentswati
  • 2
2 Solutions
 
aviadbdCommented:

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
 
avizitCommented:
>>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
 
aviadbdCommented:

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
 
stefan73Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now