You are here:

C++/Memory deallocation

Advertisement


Question
HI ,
       i have one basic doubt , in C++ we will delete the dynamically allocated object but even for normal object creation also  it will need the memory.

  why we will delete the Dynamic objects memory only ?
  how normal objects memory released ?
  what is the type of memory used by noraml objects and dynamic objetcs.
  how the   dynamically allocated memory is persistent and i will access out of its function scopes also (i think)
  why not   the same for normal objects?

  i think both normal and dynamic objects use  RAM memory only  but  why i have to relase dynamic objects memory ?

Many thanks in Advance
 sireesh

Answer
Where an object (variable) is stored and the life time of the object (variable) is determined my the storage duration of the object (variable). There are three storage durations supported by C++ : automatic, static and dynamic.

// static storage duration, modifiable
int global_var = 7 ;

// static storage duration, a constant known at compile-time
const int global_const = 9 ;

// static storage duration, constant requiring dynamic initialization
const int global_const2 = std::rand() ;


void foo( int arg )
{
// automatic storage duration, modifiable
int auto_var = 9 ;

// automatic storage duration, a constant known at compile-time
int auto_const = 9 ;

// automatic storage duration, constant, dynamic initialization
const int auto_const2 = std::rand() ;

// static storage duration, modifiable
static int static_var = 7 ;

// static storage duration, a constant known at compile-time
static const int static_const = 9 ;

// static storage duration, constant, dynamic initialization
static const int static_const2 = std::rand() ;

// int with dynamic storage duration
int* ptr = new int(100) ;
}

Objects with a static storage duration are stored in the 'static data area'; an area memory (of constant size) available as part of the program right from the beginning till the end. The life-time of these variables ends when main returns - the programmer does not have to do anything about it.

Variables with an automatic storage duration are allocated on the 'frame' of the function (typically on the stack). The life-time of these variables start when the definition is encountered and ends when the local scope containing them is exited - this is done automatically and the programmer does not have to do anything.

Variables with a dynamic storage duration come from the 'free store'. conceptually, from the pool of 'free' memory available. typically implemented using a 'heap' to hold the memory blocks. The life-time of these variables start when the programmer invokes new and ends when delete is invoked - thus, this is the only storage duration that is completely under the control of the programmer. The programmer is responsible for invoking delete when the life-time of an object with dynamic storage duration is over.

Memory for a constant known at compile-time may be optimized away by the compiler; however its address can still be taken. And if it is stored, the memory may not be writable.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.