Returns a large struct object by value or a pointer to a dynamic allocation in C ++

Here is my problem. I have a function that takes structas input, selects a new one structand then returns it. structhas the following content

struct Data {
std::vector<custom_type> vector_vars;
std::string key;
std::map<std::string, Data*> index;
};

vector_varshas a size of 500-1000. custom_type, if relevant, this class . indexhelps me access another structure Dataon key. This is a function.

Data func(Data inputData) {

 Data outputData;
//compare values with inputData
//change some values in Data struct
 return outputData
}
  • I use stack distribution and return it so that it is copied to RHS. Is this advisable since copying can create a lot of overhead?

  • Is it possible to return a link / pointer to the structure allocated on the stack inside the function?

  • ( std::shared_ptr) ?

+5
4
  • . elision - , . ++ 03 . ++ 11 (, , ), .

    return , ( catch-clause) cv- , , / ,

    , , , .

    , . , , , , , , , (. 3).

  • , . (, ) . . - undefined.

  • , , . , , Data . std::unique_ptr, ( ).

+15
  • , : outputData , . . inputData , , Data const& inputData.
  • , undefined.
  • . , , , , , , .
+2
Data func1(Data inputData) {
    Data outputData;
    return outputData
}

Data Data, . , outputData, inputData. , , , ( ). pas inputData : Data func1(const Data& inputData).

, ?

: , .

/ , ?

/ , . , , ( ), undefined

?

, . - , . RAII .

+2

func(), return Data* Data. , Data*, a Data. : ? , ?

, , " " ( " ", , ). " " : , 16 .

: " " ? , . , . , ++ , , . , , .

+1
source

All Articles