Multidimensional arrays/NULL/passing by reference

All,

I’m new to C++ and trying to figure out the best way to do a multidimensional array of pointers to a class.

The code I am presenting demonstrates several questions I had and am hoping to get some help with each question.

The basic idea is to create myMatrix, a two dimensional array of pointers to myClass, populate it by passing it to the function populateMatrix by reference, delete the array, and repeat these steps for a specific number of iterations (10 in the program presented).

I think the code below is 95% correct, although I get an error on line 67 that states "error C2512: 'myClass' : no appropriate default constructor available". I’m not certain how to fix this. Also, I’m not sure how to refer to the matrix once I pass it to the populateMatrix function.

Although those are my questions for the actual code, I was hoping to get some other thoughts on the coding in general. Specifically,
1) If I would like to repopulate the matrix with a different set of values each time (say, read the values from a file for example), should I create and delete the matrix for each iteration as shown, or is it more practical to simply set all the pointers to NULL and reuse it?

2) Should I be setting the pointer to NULL in line 70? I got the basic structure of the code from Wrox Professional C++ (page 360), but they don’t set the pointer to NULL. They way I read Beginning Visual C++ 2010, it seems to say all pointers should be set to null when initialized. Also, I thought I had read somewhere that pointers set to NULL don’t actually go through all the delete steps, so there is a time savings if the pointer is set to NULL to begin with and then never assigned a value. Is this true?

3) With the code shown, is all the memory in fact released between each iteration? I ask because it looks like I am cycling through the y dimension but not the x dimension. Again, the structure is from Wrox Professional C++, so I’m sure its correct, but I don’t understand exactly what happening here.

4) Also, I tried to use nullptr, but I get a C2065 error. The program is a native C++ program and I am using Visual Studio 2008. Is this available or should I stick with NULL?

Also, any other thoughts here would be greatly appreciated.

Max


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// MatrixTest

#include <cstring> // For the NULL

class myClass
{
public:

	// Static int to track number of values in the matrix
	static int numberOfValuesInMatrix;

	// Constructor
	myClass( int inValue ) : value( inValue )
	{
		numberOfValuesInMatrix++;
	}

	// Destructor
	~myClass(void)
	{
	}

protected:

	int value;

};

void populateMatrix( myClass**& inMyMatrix, int inIteration, int inXDimension, int inYDimension )
{

	int populateNumber;

	for (int i = 0; i < inXDimension; i++)
	{

		for (int j = 0; j < inYDimension; j++)
		{
			// Here I want to populate inMyMatrix[i][j] with inIteration * 5000 + populateNumber,
			// but I'm not sure how I refer to inMyMatrix
			// 

			populateNumber++;
		}

	}

}

int main()
{

	const int iterations = 10;
	const int xDimension = 100;
	const int yDimension = 50;

	for (int i = 0; i < iterations; i++)
	{

		// Create and initialize myMatrix
		myClass::numberOfValuesInMatrix = 0;

		myClass** myMatrix = new myClass*[xDimension];
		for (int j = 0; j < xDimension; j++)
		{

			myMatrix[i] = new myClass[yDimension]; 	// "error C2512: 'myClass' : no appropriate default constructor available"

			// Set the pointer to NULL - is this necessary?
			myMatrix[i] = NULL;

		}

		populateMatrix( myMatrix, i, xDimension, yDimension );

		// Delete
		for (int i = 0; i < xDimension; i++)
		{
			delete[] myMatrix[i]; // Do I not have to cycle through the y dimension and delete those pointers?
		}
		delete[] myMatrix;
		myClass::numberOfValuesInMatrix = 0;

	}

	return 0;

}
Last edited on
I've played around with it some more and corrected a typo in line 67 and 70 to refer to j instead of i.

Also, I changed the default constructor for myClass and created a setValue method. Now I get it to compile. New code is below.

Please note, I still have several questions concerning the programming style.

Thanks,

Max

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// MatrixTest

#include <cstring> // For the NULL

class myClass
{
public:

	// Static int to track number of values in the matrix
	static int numberOfValuesInMatrix;

	// Constructor
	myClass( void )
	{
	}

	// Destructor
	~myClass(void)
	{
	}

	void setValue( int inValue )
	{
		value = inValue;
	}

protected:

	int value;

};

void populateMatrix( myClass**& inMyMatrix, int inIteration, int inXDimension, int inYDimension )
{

	int populateNumber;

	for (int i = 0; i < inXDimension; i++)
	{

		for (int j = 0; j < inYDimension; j++)
		{
			// Here I want to populate inMyMatrix[i][j] with inIteration * 20 + populateNumber,
			// but I'm not sure how I refer to inMyMatrix
			// 
			populateNumber++;
		}

	}

}

int myClass::numberOfValuesInMatrix = 0;

int main()
{

	const int iterations = 10;
	const int xDimension = 4;
	const int yDimension = 5;

	for (int i = 0; i < iterations; i++)
	{

		// Create and initialize myMatrix
		myClass::numberOfValuesInMatrix = 0;

		myClass** myMatrix = new myClass*[xDimension];
		for (int j = 0; j < xDimension; j++)
		{

			myMatrix[j] = new myClass[yDimension]; 	// "error C2512: 'myClass' : no appropriate default constructor available"

			// Set the pointer to NULL - is this necessary?
			myMatrix[j] = NULL;

		}

		populateMatrix( myMatrix, i, xDimension, yDimension );

		// Delete
		for (int i = 0; i < xDimension; i++)
		{
			delete[] myMatrix[i]; // Do I not have to cycle through the y dimension and delete those pointers?
		}
		delete[] myMatrix;
		myClass::numberOfValuesInMatrix = 0;

	}

	return 0;

}
Last edited on
For future reference, updated and corrected code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// MatrixTest

#include <cstddef>

class myClass
{
public:
	// Static int to track number of values in the matrix
	static int numberOfValuesInMatrix;

	// Constructor
	myClass( void )
	{
	}

	// Destructor
	~myClass(void)
	{
	}

	void setValue( int inValue )
	{
		value = inValue;
	}

protected:

	int value;

};

void populateMatrix( myClass** const & inMyMatrix, int inIteration, int inXDimension, int inYDimension )
{

	int populateNumber = 0;

	for (int i = 0; i < inXDimension; i++)
	{
		for (int j = 0; j < inYDimension; j++)
		{
			inMyMatrix[i][j].setValue( ( inIteration * 20 ) + populateNumber );
			populateNumber++;
		}
	}

}

int myClass::numberOfValuesInMatrix = 0;

int main()
{

	const int iterations = 100;
	const int xDimension = 200;
	const int yDimension = 20000;

	// Iterate
	for (int i = 0; i < iterations; i++)
	{

		// Create myMatrix
		myClass** myMatrix = NULL;
		myMatrix = new (myClass*[xDimension]);
		for (int m = 0; m < xDimension; m++)
		{
			myMatrix[m] = NULL;
			myMatrix[m] = new myClass[yDimension]();
		}

		myClass::numberOfValuesInMatrix = 0;

		populateMatrix( myMatrix, i, xDimension, yDimension );

		// Delete myMatrix
		for (int i = 0; i < xDimension; i++)
		{
			delete[] myMatrix[i];
			myMatrix[i] = NULL;
		}
		delete[] myMatrix;
		myMatrix = NULL;
		myClass::numberOfValuesInMatrix = 0;

	}

	return 0;

}
Topic archived. No new replies allowed.