0

I have noticed that in gcc C11 you can pass any matrix to a function fn(int row, int col, int array[row][col]). How to translate my below placed (in a link to an another stackoverflow answer) program in C11 to a program in C++11?

C - allocating a matrix in a function

As you can see I can pass to functions static and dynamic allocated arrays in C11. Is it possible in C++11?

I have made an exemplary program based on different stackoverflow answers, but all functions work for array1 and none of them works for array2, where double array1[ROW][COL] = { { } } and auto array2 = new double[ROW][COL]() ?

How to make a function for both arrays as is made in C11 fn(int row, int col, int array[row][col])?

#include <iostream>
#include <utility>
#include <type_traits>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;

const int ROW=2;
const int COL=2;

template <size_t row, size_t col>
void process_2d_array_template(double (&array)[row][col])
{
    cout << __func__ << endl;
    for (size_t i = 0; i < row; ++i)
    {
        cout << i << ": ";
        for (size_t j = 0; j < col; ++j)
            cout << array[i][j] << '\t';
        cout << endl;
    }
}

void process_2d_array_pointer(double (*array)[ROW][COL])
{
    cout << __func__ << endl;
    for (size_t i = 0; i < ROW; ++i)
    {
        cout << i << ": ";
        for (size_t j = 0; j < COL; ++j)
            cout << (*array)[i][j] << '\t';
        cout << endl;
    }
}

// int array[][10] is just fancy notation for the same thing
void process_2d_array(double (*array)[COL], size_t row)
{
    cout << __func__ << endl;
    for (size_t i = 0; i < row; ++i)
    {
        cout << i << ": ";
        for (size_t j = 0; j < COL; ++j)
            cout << array[i][j] << '\t';
        cout << endl;
    }
}

// int *array[10] is just fancy notation for the same thing
void process_pointer_2_pointer(double **array, size_t row, size_t col)
{
    cout << __func__ << endl;
    for (size_t i = 0; i < row; ++i)
    {
        cout << i << ": ";
        for (size_t j = 0; j < col; ++j)
            cout << array[i][j] << '\t';
        cout << endl;
    }
}

int main()
{
    double array1[ROW][COL] = { { } };
    process_2d_array_template(array1);
    process_2d_array_pointer(&array1);    // <-- notice the unusual usage of addressof (&) operator on an array
    process_2d_array(array1, ROW);
    // works since a's first dimension decays into a pointer thereby becoming int (*)[COL]

    double *b[ROW];  // surrogate
    for (size_t i = 0; i < ROW; ++i)
    {
        b[i] = array1[i];
    }
    process_pointer_2_pointer(b, ROW, COL);


    // allocate (with initialization by parentheses () )
    auto array2 = new double[ROW][COL]();

    // pollute the memory
    array2[0][0] = 2;
    array2[1][0] = 3;
    array2[0][1] = 4;
    array2[1][1] = 5;

    // show the memory is initialized
    for(int r = 0; r < ROW; r++)
    {
        for(int c = 0; c < COL; c++)
            cout << array2[r][c] << " ";
        cout << endl;
    }

    //process_2d_array_pointer(array2);
    //process_pointer_2_pointer(array2,2,2);

    int info;
    cout << abi::__cxa_demangle(typeid(array1).name(),0,0,&info) << endl;
    cout << abi::__cxa_demangle(typeid(array2).name(),0,0,&info) << endl;

    return 0;
}
Community
  • 1
  • 1
42n4
  • 1,292
  • 22
  • 26
  • Why not just pass something like a `vector>&` ? – Michael Dec 13 '14 at 09:12
  • I know this template solution. But if I have had in my program these mentioned two arrays, I would like to know the correct answer. Is it possible to pass these two arrays to the same function? – 42n4 Dec 13 '14 at 09:20
  • @42n4 - Why do you need to post all of this code for such a seemingly simple question? Wouldn't a 5 line main() function calling a couple of dummy functions be sufficient to relay your point? – PaulMcKenzie Dec 13 '14 at 09:28
  • These functions are made for array1, but how to make them work for array2? These arrays have different types: double [2][2], double (*) [2]. In C11 it is possible to pass them to the same function. Is it possible in C++11? – 42n4 Dec 13 '14 at 09:32

2 Answers2

2

C++ does not have VLA. It is proposed for C++17 but there's a lot of work to do yet because it is quite a big change to the type system, and using C-style arrays is discourated in C++ anyway.

As you have found, you can use templates when the size is known at compile-time. If the size is not known at compile-time then your best bet is to use a single-dimensional vector wrapped up in a class for accessing it in the way you want to access it.

The vector of vectors is also possible of course; that describes a jagged array. Whether you prefer that over the single large memory block depends on various things (complexity of coding, runtime speed / memory usage considerations etc).

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
2

The feature that you are using in C11 was introduced in C99, and was specifically designed to allow efficient and easy handling of multidimensional arrays.

While C++ shares the basic syntax with C when it comes to (multidimensional) arrays, array types are significantly less powerfull in C++: In C++ the sizes of array types are required to be compile time constants. Here are a few examples:

void foo(int a, int b) {
    int foo[2][3];     //legal C++, 2 and 3 are constant
    int bar[a][3];     //Not C++, proposed for C++17: first dimension of an array may be variable
    int baz[a][b];     //Not C++, legal in C99

    int (*fooPtr)[2][3];    //legal C++
    int (*barPtr)[a][3];    //Not C++, legal in C99
    int (*bazPtr)[a][b];    //Not C++, legal in C99

    typedef int (*fooType)[2][3];    //legal C++
    typedef int (*barType)[a][3];    //Not C++, legal in C99
    typedef int (*bazType)[a][b];    //Not C++, legal in C99

    int (*dynamicFoo)[3] = new int[2][3];    //legal C++
    int (*dynamicBar)[3] = new int[a][3];    //legal C++
    int (*dynamicBar)[b] = new int[a][b];    //Not C++
}

As you see, almost everything that's possible in C with dynamic sized arrays is not possible in C++. Even the VLA extension that's proposed for the next C++ standard does not help much: it's restricted to the first dimension of an array.

In C++, you have to use std::vector<> to achieve what you can achieve with C99 variable length arrays. With all the consequences:

  • The data in an std::vector<std::vector<> > is not consecutive in memory. Your caches might not like this.

  • There is no guarantee with an std::vector<std::vector<> > that all line arrays have the same length. This can be useful, or a pain, depending on your use case.

  • If you have an iterator to an element in an std::vector<std::vector<> >, you can't advance it to the corresponding element in the next line.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106