please i want to return multiple values in c++ ,

and i have this peace of struct

struct returns {
int row_locs,col_locs;
int row_descriptors,col_descriptors;
double **locs;
double **descriptors;
};

and i have this pease of call by reference

 void returns (int &row_locs,int &col_locs,int &row_descriptors,int &col_descriptors,double  **locs, double  **descriptors){
 }

the question is

what doesn't consume more time struct or call by reference ??

Thanks in advanced

The answer to that is going to be entirely implementation-dependent, and the difference is probably negligible. Only by profiling can you get a definitive answer. The main cause of the difference probably will not be from the return per se, but from the need to pack and unpack the struct with the data.

If you don't mind me asking, why is this a significant concern? Worrying about this seems like premature optimization to me. It is generally more profitable to get the program working first, then look into the efficency of the different parts - and only to optimize those areas which can be empirically shown (via profiling) to be bottlenecks or hot-spots. Otherwise, you risk creating what Michael Abrash referred to as a 'fast slow program' - on which is nominally 'optimized', but doesn't run any better than the unoptimized version, because the worng parts of the code were tightened up at the expense of overall performance.

From a design and maintainability standpoint, it would probably be more fruitful to look at what you are doing with the data both before and after the function. If you find yourself using the structure anyway, or if the data is being passed around as a unit to more than one function, then by all means you would be better off with the struct. OTOH, if this is the only place where you use the structure, and it is used solely to pass back multiple values and not used in any other context, then pass by reference is the better solution. Given that the data do all seem to go together naturally, I would say that the former is more likely than the latter, though without context I just can't tell. Chances are, you would be better off focusing on the deesign than on the performance, and use the struct (or even a class) as a logical unit rather than as just a kludge for passing back return values.

commented: Agreed +14

Passing by reference or value should be pretty much the same for POD types. In this case your struct only contains POD types so passing the struct by value or passing all of the variables by reference should be the same. If you want to you can pass your struct by reference. It might give you a little bit of a boost.

void Returns(returns & returner)
{
}

Normally I'd say not to micromanage piddling stuff like this. However, C++ has gotten more quirky over the years in that seemingly piddling things can have a nontrivial effect on performance. In this case though I would be quite shocked to see any justification for either of those options other than syntactic convenience.

Profile all you want, but don't be surprised if there's little to no difference.

When you have a lot of paraemters like you originally posted it is probably better to use a structure because it's easier to read and understand. With individual parameters it can get a little messy later in the program trying to figure out where a variable is declared. With a structure there is never any doubt. It's also less likely that a global variable will be hidden by a local parameter variable with the same name, and that can cause countless debugging hours trying to figure out why the global variable isn't getting changed when you think it should (been there and done that).

If the option is between this:

int row_locs, col_locs;
int row_descriptors, col_descriptors;
double **locs;
double **descriptors;

call_function(row_locs, col_locs, row_descriptors, col_descriptors, locs, descriptors);

and this:

struct return_struct {
    int row_locs, col_locs;
    int row_descriptors, col_descriptors;
    double **locs;
    double **descriptors;
};

return_struct result = call_function();

Then, it depends on whether the function gets inlined or not. If inlined, the two will probably be identical, and I mean, literally identical (same machine code) (or maybe just a different order on the data on the stack). If not inlined, then the second option will probably perform a little bit faster. Like others have said, the performance difference is probably not worth worrying about, until you identify it as a performance issue.

The reason that the second version could be faster is just because it's almost guaranteed that the compiler can do a return-value optimization on a POD type (the struct). What RVO means is that the copy of the return value is optimized away. In other words, the return value (the struct) is constructed directly at the destination, instead of being copied. So, with RVO, there is no copy and no indirection required inside the function (indirection: de-referencing pointers or references). When passing references into a function, the compiler cannot optimize away the indirection unless the function is inlined, so, you avoid the copy for sure but you suffer from indirections (which is not significant overhead, especially if they reference stack-based variables). Passing by value and returning by value will avoid the indirections within the function but might cause copying, but in simple cases (POD-types), the copying is optimized away (and even in more complex cases (non-POD)). But again, either way, the difference is very small, if any.

From a style point of view, you should prefer to have meaningful types (struct / class) and return them by value. Those types should be meaningful in a broader context than just this one function. There's no point in making a special struct-type for each function you have, that's just bloated. And don't worry about performance until you identify the bottlenecks.

commented: Good as usual +12

If you have so many return types, you may want to consider another option. Depending on your situation a class could be the exact solution you are looking for. For example if your function just does something to those variables it could very well be best to use a class. That way you get the local this calls and only need to 'return' the values that you need through getters and setters. As such in my opinion the solution I would use is this:

class somethingDescriptive
{ //private:
    int row_locs, col_locs;
    int row_descriptors, col_descriptors;
    double **locs;
    double **descriptors;
    public:
    somethingDescriptive();
    void someFunction();
    int getRowLocs();
    int getColLocs();
    int getRowDesc();
    int getColDesc();
    int locs(int x, int y);//assuming locs is a 2d array
    int descriptors(int x, int y);//same assumption
};

Or something similar. Another option is to combine locs and descriptors into something like this:

struct Cell
{
    double loc,desc;
};
struct DimensionSpecifier//im assuming still that locs/descriptors are 2d arrays
{
    int row,col;
};

void someFunction(DimensionSpecifier &ds, Cell **cells)
{
    //todo
}

Basically in most situations other than massive projects if you have enough parameters that this is an issue, you probably have too many parameters.

I am not sure about that, but I think return through a call by reference consumes less time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.