| description | Learn more about: Memory Management: Examples | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| title | Memory Management: Examples | ||||||||||||||||
| ms.date | 11/04/2016 | ||||||||||||||||
| helpviewer_keywords |
|
||||||||||||||||
| ms.assetid | 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
newoperator with the array syntax shown in this example:[!code-cppNVC_MFC_Utilities#2]
-
Use the
deleteoperator 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
newto allocate data structures on the heap anddeleteto 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
newoperator, which returns a pointer to the object, to allocate objects on the heap. Use thedeleteoperator to delete them.The following heap and frame examples assume that the
CPersonconstructor takes no arguments.[!code-cppNVC_MFC_Utilities#7]
If the argument for the
CPersonconstructor 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]