Thread: Array with user-defined dimensions

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    16

    Question Array with user-defined dimensions

    Hi. I have a question about a program I wrote to generate a 2D "Array" with user-defined dimensions. Right now the program only asks for the dimensions, fills the elements with an ascending sequence of numbers and shows the "array". Here's the code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    	int rows;
    	int columns;
    	int i;
    	int j;
    	int counter = 1;
    		
    	cout << "Rows? ";
    	cin >> rows;
    	cout << "Columns? ";
    	cin >> columns;
    	cout << endl;
    
    	int** pRows;
    	int* pColumns;
    	if (!(pRows = new int*[rows]))
    	{
    		cout << "Not enough memory.  Sorry.\n";
    		exit(1);
    	}
    	for (i = 0; i < rows; i++, counter++)
    	{
    		if (!(pColumns = new int[columns]))
    		{
    			cout << "Not enough memory. Sorry.\n";
    			exit(1);
    		}
    		pRows[i] = pColumns;
    		for (j = 0; j < columns; j++, counter++)
    		{
    			pRows[i][j] = counter;
    			if (j == columns - 1) counter--;
    		}
    	}
    
    	for (i = 0; i < rows; i++)
    		for (j = 0; j < columns; j++)
    		{
    			cout << setw(6) << pRows[i][j];
    			if (j == columns - 1) cout << endl;
    		}
    	
    	delete[] pRows;
    	delete[] pColumns;
    	return 0;
    }
    My question is: is it enough with the two delete instructions to free all dynamically allocated memory? I don't think so, since the pointer pColumns goes through different memory allocations inside of the for loop. How can I solve this? Or should I simply ignore the memory leaks?

    Or is there any other way to create a 2D Array with user-defined dimensions?

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > delete[] pColumns;
    But that's just a temp variable, the real variable is pRows[i]

    Which leads onto
    for ( i = 0 ; i < rows ; i++ ) delete [] pRows[i];
    delete [] pRows;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    I wonder how I didn't see that.... lol! Everything is easier once someone explains it to you. Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined 2-D array problem
    By Turtal in forum C Programming
    Replies: 5
    Last Post: 12-15-2007, 01:23 AM
  2. Array Help Please (user defined size)
    By Planetx33 in forum C++ Programming
    Replies: 9
    Last Post: 04-07-2007, 04:36 PM
  3. class template user defined obj
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2004, 08:14 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Assign array size by user
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2001, 06:45 AM