다음을 통해 공유


Memory Management: Examples

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

  • An array of bytes

  • A data structure

  • An object

Allocation of an Array of Bytes

To allocate an array of bytes on the frame

  • 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.

    {
       const int BUFF_SIZE = 128; 
    
       // Allocate on the frame
       char myCharArray[BUFF_SIZE];
       int myIntArray[BUFF_SIZE];
       // Reclaimed when exiting scope 
    }
    

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

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

    const int BUFF_SIZE = 128;
    
    // Allocate on the heap
    char* myCharArray = new char[BUFF_SIZE]; 
    int* myIntArray = new int[BUFF_SIZE];
    

To deallocate the arrays from the heap

  • Use the delete operator as follows:

    delete [] myCharArray;
    delete [] myIntArray;
    

Allocation of a Data Structure

To allocate a data structure on the frame

  • Define the structure variable as follows:

    struct MyStructType { int topScore; };
    void MyFunc()
    {
        // Frame allocation
        MyStructType myStruct;
    
        // Use the struct 
        myStruct.topScore = 297;
    
        // Reclaimed when exiting scope
    }
    

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

To allocate data structures on the heap

  • Use new to allocate data structures on the heap and delete to deallocate them, as shown by the following examples:

    // Heap allocation
    MyStructType* myStruct = new MyStructType;
    
    // Use the struct through the pointer ...
    myStruct->topScore = 297;
    
    delete myStruct;    
    

Allocation of an Object

To allocate an object on the frame

  • Declare the object as follows:

    {
       CMyClass myClass;     // Automatic constructor call here
    
       myClass.SomeMemberFunction();     // Use the object
    }
    

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

To allocate an object on the heap

  • 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.

    // Automatic constructor call here
    CMyClass* myClass = new CMyClass;  
    
    myClass->SomeMemberFunction();  // Use the object
    
    delete myClass;  // Destructor invoked during delete
    

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

    CMyClass myClass("Joe Smith");
    

    The statement for heap allocation is:

    CMyClass* myClass = new CMyClass("Joe Smith");
    

See Also

Concepts

Memory Management: Heap Allocation