Clarification about dynamic allocation of a pointer-to-pointer

Mar 20, 2016 at 5:32pm
Hello.

I've this code:

1
2
3
4
int** matrix = new int*[3];
*matrix = new int[2];

matrix[0][1] = 4;


I don't understand this very well.

Matrix is a pointer to an array of pointers, right?

I don't quite understand this piece in particular:
*matrix = new int[2]

Because if I do this:
int* arr[4] = new int[4];

It gives me error because he was expecting me to initialize all the 4 pointers
Hence, something like this would've worked:

 
int* arr[4] = { new int{5}, new int{6}, new int{7}, new int{8} };


Since I'm allocating a new int FOR EACH POINTER

In the previous case, what is the meaning of this?
*matrix = new int[2]

It's a SINGLE pointer to which I allocated an array of ints.

I need some clarifications about this.
Last edited on Mar 20, 2016 at 5:34pm
Mar 20, 2016 at 5:52pm
int** matrix = new int*[3]; matrix is a pointer to an array of three pointers to integer.

*matrix = new int[2];
The next line initializes the first pointer to point to an array of two integers. Same as matrix[0] = new int[2];

matrix[0][1] = 4;
The last line changes one of the integers. Note that matrix[0] is the only valid entry so far, accessing any others is undefined behavior.
Last edited on Mar 20, 2016 at 5:52pm
Mar 20, 2016 at 6:52pm
Thanks, but then what's the problem here?

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
template<typename T>
class Matrix
{
private:
	T** matrix;
	size_t rows, columns;
public:
	Matrix(size_t _rows, size_t _columns) : rows{ _rows }, columns{ _columns }
	{
		matrix = new int*[rows];
		
		for (int i = 0; i < rows; i++)
			matrix[i] = new int[columns] {};		
	}

	T& operator[](size_t index)
	{
		return matrix[index];
	}
};

int main()
{
	Matrix<int> a(3, 2);
        
        // here the error
	cout << a[2][0];

	return 0;
}


Subscript requires array of object type
Last edited on Mar 20, 2016 at 6:57pm
Mar 20, 2016 at 7:04pm
Thanks, but then what's the problem here?

The problem seems to be that you are confusing references and pointers.

1
2
3
4
5
6
7
8
9
10
template<typename T>
class Matrix
{
    // ...

    T* operator[](size_t index)
    {
        return matrix[index];
    }
};


Mar 20, 2016 at 7:20pm
@cire alright, but if the function was supposed to return a reference (&), and I returned a pointer (matrix[index] is a pointer), why the code ran?

I mean, I was returning a pointer instead of a reference. The types mismatch. Why didn't the compiler give me an error?

Mar 20, 2016 at 7:39pm
Why didn't the compiler give me an error?

Because another error was generated first.

Instead of cout << a[2][0]; try cout << a[2]; with the erroneous definition.
Mar 20, 2016 at 7:44pm
Well If I completely delete the cout, the program runs nice and clean.

It should give me at least an error for returning something different from the function-return-type.

Strange behavior O.o

EDIT:

This only happens when using templates...
But I actually instantiated a Matrix<> object, so the template should've been compiled and the compiler should've generated an error
Last edited on Mar 20, 2016 at 7:48pm
Mar 20, 2016 at 7:50pm
Instantiating a Matrix object does not ensure that member functions which are not used are generated.
Topic archived. No new replies allowed.