1

I've just started to learn C++.

I have to declare a method that will accept variables like this ones:

int grid1[200][200]; int grid2[20][25]; int grid3[53][40]; 

How do I have to declare that parameter in the method?

I have found this declaration, but I don't know if it is useful or how can I use it:

int** a = new int*[rowCount]; 

I will copy that parameter to a member variable that will be dynamic (I think it will be in the heap, so this is why it is dynamic).

18
  • With complexity. Why do you need to pass around 2D C arrays? You're better off not... Commented Aug 4, 2019 at 13:46
  • Don't use a raw C-style array. What you want is a std::vector. Commented Aug 4, 2019 at 13:49
  • 3
    @VansFannel Found the problem! Stack Overflow doesn't scale well for teaching the basics of the language, or it's all we'd ever do. Start here. Have fun! Commented Aug 4, 2019 at 13:55
  • 1
    @JeJo I know, that's why I'm trying to stamp it out :P Commented Aug 4, 2019 at 13:57
  • 1
    int** a = new int*[rowCount]; is a relic of the 1990s Commented Aug 4, 2019 at 14:44

2 Answers 2

4

For statically sized arrays, use std::array. For dynamically sized arrays use std::vector. Don't ever use raw C arrays unless some weird situation forces it on you.

If you need a multi dimensional array, you can of course use std::vector<std::vector<int>> or similar for std::array. This is easy and convenient since you can do myarray[row][column] (and possibly good enough). But a better performing option is usually to just declare a 1D std::vector<int> with a size of "dimension 1 * dimension 2" and then, when indexing into it, do myvector[row_number * size_of_row + column]. Treating a 1D array as a 2D one is as easy as that and it is likely to perform better since it's friendlier to your CPUs prefetcher and cache hierarchy.

As for declaring a function to accept such arrays - it's straight forward. For example:

void f(const std::array<int, 666>& myarray); void f(const std::array<std::array<int, 42>, 666>& myarray); void f(const std::vector<int>& myarray); void f(const std::vector<std::vector<int>>& myarray); 
Sign up to request clarification or add additional context in comments.

Comments

2

Use std::vector and forget everything related to plain arrays.

void foo(vector<vector<int>>& v) { ... } // now you have a 100x50 double array vector<vector<int>> x; x.resize(100); for(auto& xx : x) { xx.resize(50); } foo(v); 

If you care about caching as the comment mentions, you can create a single dimensional vector and convert it to a multidimensional array, I had previous asked about that here for dynamic arrays. Or if your size is static you can simply:

vector<int> x(100); int(*yy)[3] = (int(*)[3])x.data(); // Creates a 2D array inside x yy[0][1] = 5; yy[1][2] = 4; yy[2][2] = 10; 

Doing internal calculations with rows and columns like x*col + row to manually convert a single dimension array to 2D is bad and error prone.

10 Comments

Don't use vectors of vectors. They're poison for your cache. Also, this doesn't answer the question.
@LightnessRacesinOrbit, so what would be the way to have multidimensional arrays?
@LightnessRacesinOrbit I did not ask for the cause, I asked for a solution.
@MichaelChourdakis You can easily have a 1D array and treat it as having 2D.
@LightnessRacesinOrbit 1. Make it. 2 Make it work. 3. Make it work fast. In that order. For someone who is starting to learn, a jump straight to 3 is counterproductive.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.