• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

c++ : declaring function that return multi array

Carlis

Senior member
I took up c++ today to do some numerical work. Now, I want to use some multi arrays in my functions. But I keep getting errors. My declaratiion goes something like this;

function declaration at top;

double myfunc[][](double[][] ); //line 14 is here...

...

And then the function;

double myfunc[][](double d[][]){
double out[3*N][3*N];

( do stuff to define out)

return out;
}


Apparently this wont do it.



tmain.cpp:14: error: expected unqualified-id before '[' token... and line 14 is the first one here...

How does one properly declare it?
 
There isn't really a non destructive way to return an array from a function. Especially considering what the underlying data type is, a pointer.

Instead, do something like this

void myfunc(double in[][], out[][])
{
( do stuff to define out)
}

out doesn't have to be passed in by reference or anything, it just has to be created and made the same size as in.

The other option is to use a vector instead. You can safely return a vector from a function. So it would look something like

vector< vector < double > > myFunc(vector < double > d)
{
vector < vector < double > > out;
(do stuff to out)
return out;
}
 
Aha! Now I get it. Im used to these new languages like java and python, which are designed for lazy people like my self. Trouble is, they are so damn slow for some applications. Thank you very much for the assistance.
/Carlis
 
You can't return an array declared within a function due to the scope of the variable. As soon as the function returns, any variables declared within the function are destroyed.
 
Try passing by reference instead. e.g.:

#include <vector>in
using namespace std;

void myFunction( const vector<vector<double> > &in, vector<vector<double> > &out ) {

}

/* out can be modified in myFunction, and changes will be visible to callers */

/* Don't forget to properly size in and out, e.g. */
void main() {
vector<vector<double> > in, out;
in.resize(3);
out.resize(3);
for(int k=0;k<3;k++) {
in[k].resize(3, 0.0);
out[k].resize(3, 0.0);
}

myFunction(in,out);
}

EDIT: Changed loop iterator from i to k to prevent formatting.
 
Originally posted by: degibson
Try passing by reference instead. e.g.:

#include <vector>in
using namespace std;

void myFunction( const vector<vector<double> > &in, vector<vector<double> > &out ) {

}

/* out can be modified in myFunction, and changes will be visible to callers */

/* Don't forget to properly size in and out, e.g. */
void main() {
vector<vector<double> > in, out;
in.resize(3);
out.resize(3);
for(int k=0;k<3;k++) {
in[k].resize(3, 0.0);
out[k].resize(3, 0.0);
}

myFunction(in,out);
}

EDIT: Changed loop iterator from i to k to prevent formatting.

🙂 Shh, we don't want to scare him to soon with optimization stuff. Generally I just use push_back and call it good on the resizing front. There shouldn't be too much overhead to do it that way.
 
Originally posted by: blahblah99
You can't return an array declared within a function due to the scope of the variable. As soon as the function returns, any variables declared within the function are destroyed.
Or define the internal array as static

 
Originally posted by: Cogman

🙂 Shh, we don't want to scare him to soon with optimization stuff. Generally I just use push_back and call it good on the resizing front. There shouldn't be too much overhead to do it that way.

I just wanted to ensure that OP could use the 'provided' code like an array 🙂
 
Back
Top