1

How am I supposed to pass static 2d array to function in cpp as an argument? I tried something like that:

 void foo(int (&tab)[N][N]) {
    // function body
 }

int main() {
  int n;
  cin >> n;
  int tab[n][n];
  foo(tab); // doesn't work
  return 0;
}

I get "no matching function error" when I try to call foo.

I need static arrays, because vectors are too slow for my needs. I would like to avoid declaring array with 10000 rows and columns, too. Moreover, I would want to use functions, because it will make my code readable. Is there any solution for this problem which will meet my expectations?

18
  • 1
    This shoudn't even work for a "1D array". C++ doesn't support variable length arrays. Commented Nov 22, 2018 at 20:39
  • 1
    Why not double pointer int **tab? Commented Nov 22, 2018 at 20:47
  • 2
    because vectors are too slow for my needs. – too slow for what? Commented Nov 22, 2018 at 21:03
  • 3
    @ventaquil "double pointer" - Why would you want to do that? Keep code simple - more stars usually indicate worse code, not better. Modern C++ has containers, smart pointers and more that you can use to avoid having to use low level stuff and keep things sane (and the abstractions usually optimize away). Commented Nov 22, 2018 at 21:03
  • 1
    @Mentos1105 Duh! Use one vector with size rows * columns and calculate the indexes like (row * columns + col). If you know you'll stay in the same row for the next n accesses, cache the index and just do the addition. Term to look up: Cache locality. Commented Nov 24, 2018 at 15:27

3 Answers 3

4

With cin >> n;int tab[n][n];, you declare a variable length array (i.e. an array which's dimensions are not compile-time-constants). You have two problems here: First, they are not supported by standard C++, and second they are not compatible with fixed size array parameters you introduced. If you declare your array with compile time known size, however, it will work:

#define N 10

void foo(int (&tab)[N][N]) {
    cout << tab[1][1] << endl;
}

int main() {
    int tab[N][N] = {};
    tab[1][1]=15;
    foo(tab);
    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, but size of my array is based on data from file which is read by my code earlier. So in order to avoid any slow dynamic data-types I need to declare huge 2-dimensional array and just work on part of it?
Even that's not possible, since foo will always work on a compile-time-constant dimension N. you cannot pass anything to it other than an array of dimensions [N][N]. Dynamic dimensions imply dynamically allocated arrays.
Yeah, I meant that in function body i will just use needed part of my array.
2

The classical C++ solution would involve using vectors of vectors. If it's not suitable (because you want more speed or more control over memory), you can define your own class for a square 2-D array.

One idea I used in my code is, implement it using an underlying 1-D vector, with accessor method returning a pointer.

struct My_2D_Array
{
    explicit My_2D_Array(size_t n):
        m_size(n),
        m_data(n * n)
    {
    }

    int* operator[](size_t i)
    {
        return m_data.data() + i * m_size;
    }
    size_t m_size;
    std::vector<int> m_data;
};

This not only lacks all sanity checks, and also makes bound-checked access impossible (because the accessor returns a bare pointer), but will work as a quick-and-dirty solution.

Usage in your code:

int foo(My_2D_Array& matrix)
{
    // example
    return matrix[2][3] + matrix[3][2];
}

int main()
{
    int n;
    cin >> n;
    My_2D_Array tab(n);
    foo(tab);
    return 0;
}

This idea is highly customizable - you can make the code for My_2D_Array as simple or as clever as you want. For example, if you still don't like usage of vector, even though it's 1-D, you can manage (allocate/deallocate) your memory separately, and store int*, instead of vector<int>, in My_2D_Array.

Comments

1

Just use a vector<> of vector<int>. No need for mucking around with non-standard arrays.

8 Comments

I said in post that vectors are too slow for my needs.
@Mentos1105 too slow for what? Thats a weird claim. A vector is as fast as you can get a dynamically sized array
@Mentos1105 Then use a single vector and view it as a 2D thing.
Have you measured it? You can't make broad sweeping statements such as this with out having first done some leg work to verify if a vector is indeed slower (which would be mightily surprising..) Write clean code - let the compiler do it's job.
Fully optimized? And when you say static arrays - do you mean statically sized arrays (compile time) or dynamically allocated arrays? And it's the "details" that will determine whether you get a quality answer or something else - keep that in mind for the future...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.