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

x
?
Solved

memory allocated to objects

Posted on 2004-10-13
5
Medium Priority
?
182 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
[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
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
aviadbd earned 300 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 75 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

Industry Leaders: 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!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

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