This isn't exactly a thread about a particular question, but a thread that may help people with memory dynamically allocated/deallocated during runtime. The below example goes over pointers in multiple dimensions--

#include <cstdlib>
#include <iostream>
/**
This program can be used to determine whether pointers are deleted
or not by the end of the program.
This will be a good tool to determine how to delete objects.
*/
using namespace std;

class MemoryManager{
      
      private:
             int index;
             static int x;
             
      public:
             MemoryManager(){
                  index = x++;
                  cout << "Creating class: " << index << endl;          
             }
             ~MemoryManager(){
                 cout << "Deleting value. Now " << --x;
                 cout << " objects remain."<< endl;
             }
             int getIndex(){return index;}
             
             static void showRemaining(){       
                   cout << "The total amount of existing objects are: ";
                   cout << x << endl;
             }
};

int MemoryManager::x = 0;

/*
 *Below are several cases I made up on my own for multi-dimensional pointers.
 *A fair warning: DO NOT USE ALL CASES AT ONCE! Doing so WILL cause damange to
 *the heap! Be very careful with this program.
 **/
int main(int argc, char *argv[])
{
        //To be commented out for the below cases
       /*
       MemoryManager **mm = new MemoryManager*[5];
    
       for(int i = 0; i < 5; i++)
       {
            mm[i] = new MemoryManager[2];
       }*/
    
       //Solution
       //case 1, apparantly the correct case
       /*
       {
         for(int i = 0; i < 5; i++)
         {
               delete[] mm[i]; //[rows] of the pointer mm
               //can be thought of as deleting the last array of values at the [ith] location
         }
       }*/
       
       //Memory leak
       //case 2, seems like a logical unwrapping process, but watch what happens
       /*
       {
           for(int i = 0; i < 5; i++)
           {
                  delete mm[i]; 
           } 
           delete mm;
       }*/
       
       //WARNING! Heap Damage can be done with this statement!
       //case 3, DANGEROUS! Avoid this at all costs!
       /*
       {
            for(int i = 0; i < 5; i++)
            {
                    for(int j = 0; j < 2; j++)
                    {
                            delete &mm[i][j];
                    }
            }      
       }*/
       
       //Memory leak
       //case 4, a typical and lazy approach to a problem
       /*
       { 
                 delete[] *mm;  //only gets rid of the first 2 elements located at mm[0]
                 delete mm;    //this statement literally does nothing    
       }*/
    
   // MemoryManager::showRemaining();
    
//-----------------------------------------------------------------------------


    //now to make a 5 * 4 * 3, triple dimension pointer (60 values)
    
    MemoryManager ***mm2 = new MemoryManager**[5];
    
    for(int i = 0; i < 5; i++)
    {
            mm2[i] = new MemoryManager*[4];
            
         for(int j = 0; j < 4; j++)
         {
            mm2[i][j] = new MemoryManager[3];
         }           
    }
    
    //Solution
    //case 1, the correct way
    {
         for(int i = 0; i < 5; i++)
         {
             for(int j = 0; j < 4; j++)
             {    
                  delete[] mm2[i][j]; //[rows][columns] of the pointer
                  //can be thought of as deleting the entire array of values at the [ith][jth] location
             }
         }
    }

    MemoryManager::showRemaining();
    cin.get();
    return 0;
}

I'd like to add it as a snippet but I'm sure that more scenarios can be introduced to promote memory management.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.