Skip to content

Latest commit

 

History

History
123 lines (87 loc) · 4.17 KB

File metadata and controls

123 lines (87 loc) · 4.17 KB
title Memory Management: Examples | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-windows
ms.tgt_pltfrm
ms.topic article
dev_langs
C++
helpviewer_keywords
objects [C++], memory allocation
data structures [C++]
arrays [C++], allocating resources
objects [C++], allocating memory
data structures [C++], allocating memory
examples [MFC], memory allocation
heap allocation, examples
memory allocation [C++], arrays
MFC [C++], memory management
struct memory allocation
types [C++], memory allocation
memory allocation [C++], objects
memory allocation [C++], examples
arrays [C++], memory management
frame allocation
memory allocation [C++], data structures
ms.assetid f10240f8-b698-4c83-9288-97a54318930b
caps.latest.revision 12
author mikeblome
ms.author mblome
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

Memory Management: Examples

This article describes how MFC performs frame allocations and heap allocations for each of the three typical kinds of memory allocations:

Allocation of an Array of Bytes

To allocate an array of bytes on the frame

  1. 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]

To allocate an array of bytes (or any primitive data type) on the heap

  1. Use the new operator with the array syntax shown in this example:

    [!code-cppNVC_MFC_Utilities#2]

To deallocate the arrays from the heap

  1. Use the delete operator as follows:

    [!code-cppNVC_MFC_Utilities#3]

Allocation of a Data Structure

To allocate a data structure on the frame

  1. Define the structure variable as follows:

    [!code-cppNVC_MFC_Utilities#4]

    The memory occupied by the structure is reclaimed when it exits its scope.

To allocate data structures on the heap

  1. 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]

Allocation of an Object

To allocate an object on the frame

  1. Declare the object as follows:

    [!code-cppNVC_MFC_Utilities#6]

    The destructor for the object is automatically invoked when the object exits its scope.

To allocate an object on the heap

  1. 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 CPerson constructor takes no arguments.

    [!code-cppNVC_MFC_Utilities#7]

    If the argument for the CPerson constructor is a pointer to char, the statement for frame allocation is:

    [!code-cppNVC_MFC_Utilities#8]

    The statement for heap allocation is:

    [!code-cppNVC_MFC_Utilities#9]

See Also

Memory Management: Heap Allocation