| title | Memory Management: Examples | Microsoft Docs | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ms.custom | |||||||||||||||||
| ms.date | 11/04/2016 | ||||||||||||||||
| ms.reviewer | |||||||||||||||||
| ms.suite | |||||||||||||||||
| ms.technology |
|
||||||||||||||||
| ms.tgt_pltfrm | |||||||||||||||||
| ms.topic | article | ||||||||||||||||
| dev_langs |
|
||||||||||||||||
| helpviewer_keywords |
|
||||||||||||||||
| ms.assetid | f10240f8-b698-4c83-9288-97a54318930b | ||||||||||||||||
| caps.latest.revision | 12 | ||||||||||||||||
| author | mikeblome | ||||||||||||||||
| ms.author | mblome | ||||||||||||||||
| manager | ghogen | ||||||||||||||||
| translation.priority.ht |
|
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 and delete 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 the delete operator 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]