I am currently browsing over some old college snippets of c++ code. Back then, one of the other class was assigned with doing a matrix class using double pointers and 2D arrays. Luckily (or unluckily upon hindsight) I never did get a chance to learn stuff like that. I borrowed their code when we graduated for future review. If anyone can please explain to me what exactly happens in this snippet:
//This is a constructor of a 1x1 matrix
signal::signal(){
_nrows = 1;
_ncols = 1;
_coef = new double*[_nrows];
_coef[0] = new double[_ncols];
_coef[0][0] = 0.0;
}
Just a sidenote, _coef is a ** of type double.
From what I understand, _nrows and _ncols are given a value of 1 (meaning their sizes). Then, the code dynamically creates a double* out in the heap with elements equal to _nrows; the problem is, I dont exactly know what happens next. Why is the array corresponding to _ncols not a pointer? Why is it assigned _coef[0]?
In memory, a two dimensional array (n, m) looks more or less like this
_coefpoints to an array ofnpointers. And each of these pointers point to an array ofmdoubles.So, in your case
_coefpoints to an array of 1 pointer and this pointer points to an array of one double.Now to your questions
_coef[0], because it is the first, and only, row of your two dimensional array.