Returning multiple data items from a function in C or C ++

I got confused in several homework that I have ... Can you return multiple data items from a function using return ()? Can a function return only one value if it is not a pointer to an array?

I believe the answer is that a function can return multiple data items, returning a structure. Then returning a pointer to an array is not the only way - if it is a way?

But there seems to be a lot of discussion on this topic, so I want to make sure that I have at least the main idea: you can return multiple data elements using a structure, but using a pointer (I don't understand this) will use memory more effectively. Is it correct?

+8
c ++ return structure
source share
5 answers

With C ++ 0x / C ++ 11 you can use this:

#include <string> #include <iostream> #include <tuple> using namespace std; tuple<int, unsigned, string, int> getValues() { return tuple<int, unsigned, string, int>(1, 2, "3", 4); } int main() { int a; unsigned b; string c; int d; tie(a, b, c, d) = getValues(); cout << a << ", " << b << ", " << c << ", " << d << endl; return 0; } 

Compile it with

 g++ tuple_test.cpp -std=c++0x -o tuple_test 

And if you run the program, it will output this:

 1, 2, 3, 4 

But it is better to use such links (I would say):

 void getValues(int& a, unsigned& b, string& c, int& d) { a = 1; b = 2; c = "3"; d = 4; } int main() { ... getValues(a, b, c, d) ... } 

Uch is what I get for not carefully reading the question ...

Can a function return only one value if it is not a pointer to an array?

Yes, you can return only one value, but this single value can include multi-valued values ​​(struct, class, array).

You can return multiple data elements using a structure, but using a pointer (I don't understand this) uses memory more efficiently. Is it correct?

True But when you use pointers, it depends on how you use it. When you dynamically allocate it, each function call will not be very efficient, and you will need to free memory manually after use. When you use a global array / structure, it will be efficient. But it can cause problems when you call the function to multiply time.

+7
source share

In addition to what has already been said in this thread, in C ++ 11 you can return structures initialized using uniform initialization :

 struct XYZ { int x; int y; int z; }; XYZ foo() { return {1, 2, 3}; } 

Personally, I prefer to return structures with named members, rather than tuples, because the latter does not provide names for its members.

+3
source share

It is right.

However, you can β€œreturn” several elements by adding parameters that are passed by reference and then write several results to them.

+1
source share

A function can really only return one thing with its return statement. However, this thing can be a pointer (C and C ++ arrays are just masking pointers), a link (a pointer that cannot be reinstalled or indexed by an array), or an object that can encapsulate several things.

If you return the structure, you will pass the entire structure. If you return a pointer or link, you only return the address of the structure - so you better not return a link or pointer to a structure that goes outside the scope when the function returns! This causes undefined behavior, which is most likely (but not always) a segmentation error.

+1
source share

If you want a more detailed picture about this to read about passing parameters by value and passing by reference, it is also used to return parameters.

As you mentioned:

You can return multiple data elements using a structure, but using a pointer (I don't understand this) uses memory more efficiently. Is it correct?

Suppose you have a structure:

 struct BigStructure{ int array[1000]; double otherData[1000]; //... etc }; 

and some method:

 BigStructure workWhenCopying(BigStructure bigStruct){ // some work with a structure return bigStruct; } 

This method illustrates the case when you pass parameters to a method and return it by value, which means that every time you call this method, you copy the method argument to another location in memory. This copying takes time, and when you have large structures, it slows down the execution time of your program, therefore it is more efficient to pass large structures by reference.

 void workWithReference(BigStructure & bigStruct){ // do some work } 

In this case, you are not copying the whole data structure, you simply pass the memory address in which this structure is located. You also do not need a return statement, since changes to this structure object will be visible outside this method. But if you reassign bigStruct to a new memory location, it will only be visible inside the local method: workWithReference

I hope this becomes clearer now;)

0
source share

All Articles