Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function that returns multidimensional array in C++

Being a Java-programmer, I have a hard time getting a function to return a multidimensional array. How would i code this in C++?:

int[][] theFunction(){
  int[][] var = new int[3][3];
  // code
  return var;
}
like image 225
user1919607 Avatar asked Dec 20 '12 17:12

user1919607


People also ask

What is a multidimensional array in C programming language?

C programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration − For example, the following declaration creates a three dimensional integer array − The simplest form of multidimensional array is the two-dimensional array.

How do you return a 2D array in C++?

Use Pointer Notation to Return 2D Array From Function in C++ Return by the pointer is the preferred method for larger objects rather than returning them by value. Since the 2D array can get quite big, it’s best to pass the pointer to the first element of the matrix, as demonstrated in the following code example.

How to return a variable from an array in C++?

In C/C++, when you pass an array to a function, it decays to be a pointer pointing to first element of the array. So, in pixels () function, you are returning the address of a stack allocated variable. The returning variable's address is no longer valid because on pixels () return, the stack allocated variable goes out of scope.

How to return a single-dimension array from a function in Python?

If you want to return a single-dimension array from a function, you would have to declare a function returning a pointer as in the following example − int * myFunction() { . . .


2 Answers

In C++, the easiest way of doing it is like this:

std::vector<std::vector<int> > theFunction() {
    std::vector<std::vector<int> > var(3, std::vector<int>(3));
    // code
    return var;
}

You need to include <vector> in order for this to compile. Unlike Java generic containers, C++ template containers do not incur the cost of wrapping primitives into objects, so they can stay extremely efficient in terms of performance and memory consumption while providing a great deal of additional flexibility.

In general, you should prefer C++ - style containers (std::vector, std::set, std::map, and in C++11, std::array) to their less flexible built-in alternatives "inherited" from C.

like image 80
Sergey Kalinichenko Avatar answered Sep 21 '22 22:09

Sergey Kalinichenko


In short, in C (which is the, let's say, dialect you're using from C++ [C++ is a superset of C, with some modifications]) you cannot return a vector nor matrix from a function. You can, though, return a pointer (and probably that's not going to help you very much).

In C and C++, the name of the vector (let's simplify it) is a pointer to the first position, so:

int v[] = { 1, 2, 3 };
int * ptr = v;

A pointer is a memory address, you can use that in order to run over all elements (though it can be dangerous):

for( ; ptr < ( v + 3 ); ++ptr) {
     std::cout << *ptr << stD::endl;
}

And that pointer can be returned:

int * vectorCreator(int max)
{
    int * v = new int[max];
    return v;
}

Beware that, in this case, the caller takes the responsibility of freeing the vector once is done. This is a problem you can solve with auto_ptr (which is obsolete with the new standard, you should use unique_ptr once your compiler allows it).

auto_ptr<int> vectorCreator(int max)
{
    int * v = new int[max];
    return auto_ptr<int>( v );
}

In this very direction works part of the C++ standard library, in which you can use the vector<> template, which is safer, and definitely more comfortable.

Hope this helps.

like image 31
Baltasarq Avatar answered Sep 20 '22 22:09

Baltasarq