0
int *x = new int[5]();

With the above mentality, how should the code be written for a 2-dimensional array - int[][]?

int **x = new int[5][5] () //cannot convert from 'int (*)[5]' to 'int **'

In the first statement I can use:

x[0]= 1;

But the second is more complex and I could not figure it out. Should I use something like:

x[0][1] = 1;

Or, calculate the real position then get the value for the fourth row and column 1

x[4*5+1] = 1;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
jack-london
  • 1,599
  • 3
  • 21
  • 42
  • 1
    You should *not* be doing manual memory management. Not only is your code messy since you have to remember to delete everything, it's unsafe because you might forget, or have an exception thrown. Use `std::vector`, they can be nested. – GManNickG Jul 29 '10 at 23:00

6 Answers6

4

I prefer doing it this way:

int *i = new int[5*5];

and then I just index the array by 5 * row + col.

Simon
  • 773
  • 3
  • 11
  • Very nice, this avoids the inefficiency of multiple allocations. You can wrap this in a class that hides things by doing the index calculation for you. – Jon Reid Jul 29 '10 at 22:49
  • ...and then you can arrive to the solution from C++ FAQ Lite: http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10 – Cubbi Jul 29 '10 at 23:28
  • I agree, wrapping it in a class in order to do row/col access is very nice. – Simon Jul 30 '10 at 07:18
2

You can do the initializations separately:

int **x = new int*[5];
for(unsigned int i = 0; i < 5; i++)
    x[i] = new int[5];
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
2

There is no new[][] operator in C++. You will first have to allocate an array of pointers to int:

int **x = new int*[5];

Then iterate over that array. For each element, allocate an array of ints:

for (std::size_t i = 0; i < 5; ++i)
    x[i] = new int[5];

Of course, this means you will have to do the inverse when deallocating: delete[] each element, then delete[] the larger array as a whole.

Jon Reid
  • 20,545
  • 2
  • 64
  • 95
1

This is how you do it:

int (*x)[5] = new int[7][5] ;

I made the two dimensions different so that you can see which one you have to use on the lhs.

TonyK
  • 16,761
  • 4
  • 37
  • 72
0

Ff the array has predefined size you can write simply:

int x[5][5];

It compiles

If not why not to use a vector?

kuszi
  • 2,069
  • 29
  • 36
  • yes, the array is not static-sized. Just learning core elements. Like you mentioned the effective way is with stl. – jack-london Jul 30 '10 at 00:21
  • To answer your question, using nested vectors can be quite inefficient, if you use push_back(). That's because reallocating the underlying memory for the outermost vector will trigger reallocations for the vectors it contains. – Jørgen Fogh Jun 03 '11 at 11:42
  • If it's going to get resized frequently, I would recommend that you look at a `std::list`. It'll be less efficient for element retrieval but changing the size of the data will be (relatively) cheap. – Jonathan Grynspan Sep 29 '11 at 12:29
0

There are several ways to accomplish this:

  • Using gcc's support for flat multidimensional arrays (TonyK's answer, the most relevant to the question IMO). Note that you must preserve the bounds in the array's type everywhere you use it (e.g. all the array sizes, except possibly the first one), and that includes functions that you call, because the produced code will assume a single array. The allocation of $ new int [7][5] $ causes a single array to be allocated in memory. indexed by the compiler (you can easily write a little program and print the addresses of the slots to convince yourself).

  • Using arrays of pointers to arrays. The problem with that approach is having to allocate all the inner arrays manually (in loops).

  • Some people will suggest using std::vector's of std::vectors, but this is inefficient, due to the memory allocation and copying that has to occur when the vectors resize.

  • Boost has a more efficient version of vectors of vectors in its multi_array lib.

In any case, this question is better answered here: How do I use arrays in C++?

Community
  • 1
  • 1
blais
  • 687
  • 7
  • 9