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;
1
  • 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. Commented Jul 29, 2010 at 23:00

6 Answers 6

4

I prefer doing it this way:

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
...and then you can arrive to the solution from C++ FAQ Lite: parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10
I agree, wrapping it in a class in order to do row/col access is very nice.
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];

Comments

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.

Comments

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.

Comments

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?

3 Comments

yes, the array is not static-sized. Just learning core elements. Like you mentioned the effective way is with stl.
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.
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.
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++?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.