Hello guys,

I am trying to declare a two dimensional array in C++ on runtime. For this purpose I am asking the user about how many rows and columns should be created in the array. I have written the following code for this purpose. Apparently it is working just fine. But I am asking you if you can identify any mistake or error in this code especially in the memory deallocation part. Am I correctly releasing all the memory that I first allocated at the beginning of the program. Actually my course instructor touched this topic very slightly and also unfortunately I was not able to find any clearly explained and easy to understand article on this topic online. So, I thought that it would be better to ask someone more experienced if this code is fine or not. Also if you can think of any better way of doing any of the steps in this program then please let me know. I shall be very thankful to you.

#include <iostream>
using namespace std;

int main()
{
	int cols, rows;
	cout << "How many rows do you need in your matrix? ";
	cin >> rows;
	cout << "And ... How many columns? ";
	cin >> cols;
	
	//...............memory allocation..................//
	int **myMatrix;
	myMatrix = new int*[rows];
	for (int i=0; i<rows; i++)
		myMatrix[i] = new int[cols];
	//..................................................//
	
	//............................Input..............................//
	cout << "Now fill up your " << rows << "x" << cols << " matrix:\n";
	for (int i=0; i< rows; i++)
		for (int j=0; j<cols; j++)
			cin >> myMatrix[i][j];
	//...............................................................//

	//............................Output.............................//
	cout << "\nYou entered the following matrix\n";
	for (int i=0; i<rows; i++)
	{
		for (int j=0; j<cols; j++)
			cout << myMatrix[i][j] << " ";
		cout << endl;
	}
	//...............................................................//
	
	//.................memory deallocation..............//
	for (int i=0; i<rows; i++)
		delete [] myMatrix[i];
	delete [] myMatrix;
	//..................................................//
	
	system("pause");
	return 0;
}

Edited 5 Years Ago by Muhammad Anas: n/a

I don't see anything wrong. I would suggest for good practice also setting your pointers to null when you deallocate them. I sometimes like to define delarr in a preprocessor macro:

#define delarr(X) (delete[](X));((X)=0)

Hope this helps. (Also I may be wrong so more people should check as well)

I don't see anything wrong. I would suggest for good practice also setting your pointers to null when you deallocate them. I sometimes like to define delarr in a preprocessor macro:

#define delarr(X) (delete[](X));((X)=0)

Hope this helps. (Also I may be wrong so more people should check as well)

If good practice is what your after, then it would be a good practice to use standard containers.

Why didn't you write a class Matrix???

That's a logical next step, but trying a matrix class without understanding the underlying dynamic memory logistics would be frustrating at best.

The code looks okay--assuming no errors or exceptions occur. However, better code would deal with the possibility that errors or exceptions might occur and, if they do, deal with them gracefully. There are three examples posted off the following page:

Dynamic Arrays in C++ - Three Examples

You might want to check them out.

@Narue
Oh thankyou very much for the appreciation ...

@Labdabeta
Sorry ... I really forgot to set the pointer to null. I was told to do so by my instructor too.. thanks for mentioning. I shall take care of it next time!!!

@David8
Hummm ... yes I ofcourse assumed that so much memory will not be requested which will not be available on the PC. Actually I was willing to be proficient with especially
1) how these "pointers to pointers" work in the context of memory allocation and 2) how the memory allocated using them will be properly released back to the system.
However thanks for pointing out this. I will try to learn this exception handling very soon.

This question has already been answered. Start a new discussion instead.