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;
}
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.
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.
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.
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() { . . .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With