Memory management is important for every programming language. The memory management may be Manual or Automatic. The automatic memory management is a Garbage Collection Technique. Garbage collection is a technique use to deallocate memory automatically when control flow goes to out of scope. The programmer doesn't concentrate on freeing the memory if the programming language supports garbage collection.
Java and .NET enabled languages support the garbage collection features.
The manual memory management is the technique where the memory is controlled by
the programmer. Programming languages like C or C++ use manual memory management. We allocate memory using C run time library functions or C++
new operator. C++ provides programmers ways to allocate and deallocate memory using
Memory management in C/C++:
Memory management in C uses
calloc functions to allocate the memory. The
free function is used to free the memory. The
is used to allocate the block of memory in heap. The
is to reallocate the memory; if programmer allocates the memory using
malloc, if he/she wants
to extend the memory use
realloc function. The
calloc function is to allocate the array of memory and initialize to 0. Microsoft C supports debug versions of each
realloc functions. The free function
is to free the memory. The free function also supports debug version. The debug version functions only arise if the compiler set to debug mode. Microsoft C supports extended memory using extended functions.
Some programs continually allocate memory without ever giving it up and eventually run out of memory. It is called Memory Leak. If the programmer allocates memory using
new or C run time library functions and he/she doesn't remember to release the memory in that situation memory leak
has occurred. So, the programmer handles memory efficiently and effectively.
Programming language like C is meant for allocate and reallocate features. But, in C++ there are no equivalent operators (Refer Stroustrup style and Technique FAQ Page). So, we use
new operator to extend the memory. When programmer uses vector elements (Static template Library), the user need not be concerned with the allocation and deallocation of memory. Because, the vector automatically destroy the memory when it goes to out of scope.
The manual memory management takes lot of code. The memory controlled by the programmer. So, we effectively control the memory. C/C++ supports the garbage collector explicitly using any of garbage collector.
C++ memory Management Techniques:
Microsoft suggested don't intermix with C runtime library and C++
delete operators. Suppose, if you allocate memory using new, you compulsorily
deallocate memory using
delete operator. Don't try to allocate memory-using
malloc and deallocate with
delete operator and vice versa. But, we use both
new in single program (Refer Stroustrup style and Technique FAQ Page)
new operator is used to allocate the memory.
For example, allocate the memory for integer,
Int *var = new int;
The programmer also allocate array of memory using
new operator. For example,
Int *a= new int;
delete  a;
What is difference between
Int a; and
int *a = new int. If we use
int a, the variable is allocated memory space and deallocated when it goes out of scope. But, if we use second method the programmer explicitly mentions the
delete operator to delete the variable in the memory.
The heap allocation is a portion of memory reserved for a program to use for the temporary storage. The heap allocation size cannot be known until the program is running. Stack is a region of reserved memory in which programs store status data. This status data maybe procedure and function addresses, parameters or local variables.
The C++ is a object oriented programming language. So, it supports polymorphism, encapsulation, data abstraction and inheritance and more. C++ provides programmers special member function constructor and destructor with in class definition. The contractor and destructor are the special member function to initialize the data in constructor and
deallocate data from destructor.
MFC Memory Management:
MFC Support two types of memory management. Frame allocation and Heap allocation. The frame allocation objects automatically deleted when it goes out of scope. But,
in the heap allocation the programmer deletes the object by using
delete operator explicitly.
Frame allocation allocates object within scope. Frame variables are like automatic variable. Automatic variable is the variable is only available within the function or body in the braces. For example,
int nVariable1, nVraiable2;
Myfunction contains the
fVariable. These variables are accessed only within the function. If we try to access out of this function, the compiler gives on error message.
The only advantage in automatic memory management, the programmer doesn't concentrate on memory deal location. The frame allocation uses in the stack. The stack is the temporarily store data in local scope.
MFC heap allocation uses to allocate memory using new operator and delete use delete operator. The objects lifetime is controlled by the programmer. If programmer doesn't remember to
deallocate memory the memory leak problem occurs. So, programmer handles memory carefully. For example, heap allocation,
CMyClass *objClass = new CmyClass();
So, the programmer deletes explicitly object class.
CMemoryState class for detecting the memory leak.
CMemoryState has no base class. It is used to detect the memory leak in MFC based programs.
CMemoryState is available for only debug version only. The
CMemoryState contains the afx.h header file. We use the
CMemoryState within the
#if defined (_DEBUG) and
#endif Macro. Because, the
CMemoryState uses debug mode only.
CMemoryState provides the functions to check the memory state and see the difference between the checkpoints. The following example
is to find the difference in memory leak.
CMemoryState msOld,msnew, diffMemState;
#if defined (_DEBUG)
CMyClass* obj = new CMyClass();
#if defined (_DEBUG)
if( diffMemState.Difference(msOld, msnew) )
TRACE( "Memory leaked!\n" );
Microsoft suggests don't use to
realloc for resize the memory. If you try to allocate object use
new and reallocate object-using
realloc the result will be corrupted in debug version in MFC.
The memory management is a complex field. I explained the fundamental concepts in memory management in C/C++ and MFC. I didn't explain the more
complex details in memory management and Win32 memory management. The .Net enabled languages use garbage collection techniques. So, the memory management
is done automatically.
Selvam has worked on several technologies like Core Java, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoy writing them too. He has been awarded as a Microsoft Community Star in 2004, MVP in 2005-06, SCJP 5.0 in 2009, Microsoft Community Contributor(MCC) 2011.
Web site: http://www15.brinkster.com/selvamselvam/