Deletion of 2-d dynamic array

This is a discussion on Deletion of 2-d dynamic array within the C++ Programming forums, part of the General Programming Boards category; I have an object called RobotData with, amongst others, the following data members, Code: int **PrevPosX; int **CurPosX; int **NextPosX; ...

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801

    Deletion of 2-d dynamic array

    I have an object called RobotData with, amongst others, the following data members,

    Code:
    int **PrevPosX;
    int **CurPosX;
    int **NextPosX;
    int **PrevPosY;
    int **CurPosY;
    int **NextPosY;
    The contructor for the object is here,

    Code:
    RobotData::RobotData (int a, int b):BotWidth (a), BotLength (b), velocity(0),direction(BotN),fRobotStatus(ROBOT_STATIONARY),MovementOverflow(0),fRobotTasked(NO_TASK_PRESENT),DistanceToStepsCovered (0),RobotHasMoved (0)
    {
    	PrevPosX = new int* [a];
    
    	PrevPosY = new int* [a];
    
    	CurPosX = new int* [a];
    
    	CurPosY = new int* [a];
    
    	NextPosX = new int* [a];
    
    	NextPosY = new int* [a];
    
    	for (int i = 0; i < a; i++)
    	{
    		PrevPosX [i] = new int [b];
    
    		PrevPosY [i] = new int [b];
    
    		CurPosX [i] = new int [b];
    
    		CurPosY [i] = new int [b];
    
    		NextPosX [i] = new int [b];
    
    		NextPosY [i] = new int [b];
    	}
    }
    and the destructor is here,

    Code:
    RobotData::~RobotData()
    {
    	for (int i = 0; i < BotWidth; i++)
    	{
    		delete [] PrevPosX [i];
    
    		delete [] PrevPosY [i];
    
    		delete [] CurPosX [i];
    
    		delete [] CurPosY [i];
    
    		delete [] NextPosX [i];
    
    		delete [] NextPosY [i];
    	}
    
    	delete [] PrevPosX;
    
    	delete [] PrevPosY;
    
    	delete [] CurPosX;
    
    	delete [] CurPosY;
    
    	delete [] NextPosX;
    
    	delete [] NextPosY;
    
    }
    The destructor is called when my programme receives the WM_DESTROY message (it's a windows app but I thought the problem was more C++ related so posted it here). My program closes and then I get the message telling me that an error has occured in my program. If I comment out the destructor then I get no error (not sure why that is, I thought windows implicity called your object destructors upon program closure if you had been too lazy to do it yourself).

    Initially the arrays in this object were created as normal with global variables for the size. This was sloppy though so I changed it to dynamic allocation. Then the problems above occured so I think the problem is with this dynamic allocation. Can anyone spot any obvious errors?

    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    This code works:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int** pArray;
    	int size;
    
    	cout<<"Input array size: ";
    	cin>>size;
    	
    	pArray = new int*[size];
    
    	for(int i=0; i<size; i++)
    	{
    		pArray[i]=new int[5];
    	}
    
    	for(i=0; i<size; i++)
    	{
    		delete [] pArray[i];
    		
    	}
    	delete [] pArray;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 02:59 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 07:18 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21