description | title | ms.date | helpviewer_keywords | ms.assetid | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: Memory Management: Examples |
Memory Management: Examples |
11/04/2016 |
|
f10240f8-b698-4c83-9288-97a54318930b |
This article describes how MFC performs frame allocations and heap allocations for each of the three typical kinds of memory allocations:
-
Define the array as shown by the following code. The array is automatically deleted and its memory reclaimed when the array variable exits its scope.
[!code-cppNVC_MFC_Utilities#1]
-
Use the
new
operator with the array syntax shown in this example:[!code-cppNVC_MFC_Utilities#2]
-
Use the
delete
operator as follows:[!code-cppNVC_MFC_Utilities#3]
-
Define the structure variable as follows:
[!code-cppNVC_MFC_Utilities#4]
The memory occupied by the structure is reclaimed when it exits its scope.
-
Use
new
to allocate data structures on the heap anddelete
to deallocate them, as shown by the following examples:[!code-cppNVC_MFC_Utilities#5]
-
Declare the object as follows:
[!code-cppNVC_MFC_Utilities#6]
The destructor for the object is automatically invoked when the object exits its scope.
-
Use the
new
operator, which returns a pointer to the object, to allocate objects on the heap. Use thedelete
operator to delete them.The following heap and frame examples assume that the
CPerson
constructor takes no arguments.[!code-cppNVC_MFC_Utilities#7]
If the argument for the
CPerson
constructor is a pointer tochar
, the statement for frame allocation is:[!code-cppNVC_MFC_Utilities#8]
The statement for heap allocation is:
[!code-cppNVC_MFC_Utilities#9]