Solved

memory allocated to objects

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

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

Suggested Solutions

Title # Comments Views Activity
How to gracefully close the c++ 11 thread? 3 155
How to convert MFC::CString to UTF8 wchar_t* 10 444
DCT of  2D array using fftw in c++ 9 123
Bitwise and to sum elements 2 48
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

739 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