954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamically declaring two dimensional aray - Any mistake in this code??

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;
}
Muhammad Anas
Light Poster
39 posts since Jun 2010
Reputation Points: 7
Solved Threads: 0
 
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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)

Labdabeta
Posting Pro in Training
489 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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

jencas
Posting Whiz
366 posts since Dec 2007
Reputation Points: 395
Solved Threads: 71
 
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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

DavidB
Posting Whiz in Training
213 posts since Jul 2006
Reputation Points: 48
Solved Threads: 10
 

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

Muhammad Anas
Light Poster
39 posts since Jun 2010
Reputation Points: 7
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: