domlao
domlao

Reputation: 16029

The right way to create pointer to pointer object?

What is the right way to create a pointer to pointer object? Like for example,

int **foo;
foo = new int[4][4];

Then the compiler gives me an error saying "cannot convert from int (*)[4] to int **.

Thanks.

Upvotes: 0

Views: 256

Answers (2)

Filip Navara
Filip Navara

Reputation: 4828

int **foo = new int*[4];
for (int i = 0; i < 4; i++)
   foo[i] = new int[4];

Clarification:

In many languages the code above is called a jagged array and it's only useful when the "rows" have different sizes. C++ has no direct language support for dynamically allocated rectangular arrays, but it's easy to write it yourself:

int *foo = new int[width * height];
foo[y * height + x] = value;

Upvotes: 10

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507205

Using raw new is a bit unwieldy to use. The inner dimension (last 4) must be a compile time constant, in addition. You also have to remember to delete the array once you are finished using it.

int (*foo)[4] = new int[4][4];
foo[2][3] = ...;
delete[] foo;

If that feels too "syntactic braindead", you can use typedef to prettify it

typedef int inner_array[4];
inner_array *foo = new int[4][4];
foo[2][3] = ...;
delete[] foo;

That's called a rectangular 2-dimensional array, because all the rows (4 of them, which can be determined at runtime) have the same width (which must be known at compile time).

Alternatively, use std::vector, with which you don't need to mess around with delete anymore, and which will also handle the raw pointer mess:

std::vector<int> v(4 * 4);
v[index] = ...;

You may later add or remove integers from the vector as you wish. You could also create a vector< vector<int> >, but i find it unwieldy to use, because you have to manage the separate row-vectors (which can be of varying lengths), and they are not seen as "one unit" together.

You can always create a function that maps a two dimensional coordinate to a one-dimensional index

inline int two_dim(int x, int y) {
  return y * 4 + x;
}

v[two_dim(2, 3)] = ...;

For a simple two-dimensional array whose size you know beforehand, you don't need new at all, though

int x[4][4]; 
x[2][3] = ...;

Upvotes: 8

Related Questions