0

I was reading a program about BTree, there I came across this : BTreeNode **C. I understand that it is a 2d array but it was initialized as C=new BTreeNode *[2*t];. I can't understand this: is this a 2d array with dynamic rows and 2t columns ? Thanks.

Oleiade
  • 6,156
  • 4
  • 30
  • 42
Nikhil Verma
  • 1,777
  • 1
  • 20
  • 41

3 Answers3

8

You probably well know that double* is a pointer to a double element. In the same way, double** is a pointer to a double* element, which is itself a pointer. Again, double*** is a pointer to a double** element, and so on.

When you instanciate an array to a type T, you usually do new T [size];. For example, for an array of double, you write new double[size];. If your type T is a pointer itself, it's exactly the same : you write new double*[size];, and you get an array of pointers.

In your case, BTreeNode* is a pointer to BTreeNode, and BTreeNode** is a pointer to BTreeNode* which is a pointer to BTreeNode. When you instanciate it by doing new BTreeNode*[size]; you get an array of pointers to BTreeNode elements.

But actually, at this step you don't have a 2D array, because the pointers in your freshly allocated array are NOT allocated. The usual way to do that is the following example :

int num_rows = 10;
int num_cols = 20;
BTreeNode** C = new BTreeNode*[num_rows];
for(int i = 0; i < num_rows; i++)
{
  // Then, the type of C[i] is BTreeNode*
  // It's a pointer to an element of type BTreeNode
  // This pointer not allocated yet, you have now to allocate it
  C[i] = new BTreeNode [num_cols];
}

Don't forget to delete your memory after usage. The usual way to do it is the following :

for(int i = 0; i < num_rows; i++)
  delete [] C[i];
delete [] C;
Caduchon
  • 4,574
  • 4
  • 26
  • 67
  • Not that there's anything _technically_ wrong with this, but I think "the usual way" is to use a `vector` and let that handle all the manual memory management cruft. I thought we were past end-users typing `new` and `delete`, but this pattern seems to crop up all the time. It seems more like a barely translated holdover from C than anything else. Whereas a `vector` of `vector`s would seem to achieve the same thing while not needing us always to remember to `new` and `delete` properly. – underscore_d Apr 21 '16 at 09:35
  • I not agree with you. First, the PO is reading an existing code, he's not writing something. Second, in some cases, it's clearly required, like for using blas and lapack libraries. – Caduchon Apr 21 '16 at 11:53
  • If the OP is not writing something, then why did you tell them how to write this? And sure, it can be required, but that's the exception rather than the rule; "the usual way" is different from "in some cases", after all. All I meant was the best recommendation is to use stdlib abstractions wherever possible, not manual memory management, unless you're an stdlib developer or have a proven need to do things manually. I mention this in case the OP _thinks_ they need `new`/`delete` but maybe don't. – underscore_d Apr 21 '16 at 11:57
  • 1
    To learn something more than the question ? But you can only answer striclty to questions if you want and let people do usual mistakes. Moreover, it's the usual way in a context of allocation with a `new`, yes. – Caduchon Apr 21 '16 at 12:00
  • 1
    For sure, if someone is already talking about `new`, it's always good to mention `delete`, since it often gets forgotten :-) – underscore_d Apr 21 '16 at 12:01
0

The statement C=new BTreeNode *[2*t]; allocates space for 2*t instances of type BTreeNode * and therefore returns a type BTreeNode ** pointing to the first element of such instances. This is the first dimension of your array, however no memory has been allocated for the second dimension.

gorilon
  • 424
  • 3
  • 12
0

Yes. If the array is being indexed column-major order (so C[3][4] is the 5th element of the 4th column) then C could, potentially, have ragged (differently sized) columns.

Look for some code that allocates memory for each column, i.e.

C[i] = new BTreeNode[length];

in a loop over i, that would indicate that the 2D array has the same length per column.

Ben Braun
  • 418
  • 4
  • 10