Wednesday 15 June 2011

Memory footprint of C++ functions returning by reference or value? -


I'm mostly trying to reduce the memory footprint of the application developed in C ++ and one of the things that I He was returning back, especially by returning the value by reference to the larger data structure context. I need some confirmation on this, for example, say that I have member functions in a class some_object :

  const some_type & amp; Get_some_type (...) {...; Return ...; }   

vs

  const some_type get_some_type (...) {...; Return ...; }   

Is my understanding correct, that by calling the latter (back by value), two copies of assignment data should be left in the code at some points in the some_type memory Will execution, while avoiding calling (return by context) calls?

Another attempt as part of the overall goal of reducing the memory footprint was to replace a definition of some_type which would prevent the use of the refund by the above mentioned reference option In such a case, some_type (which is actually a type of container, says type data of data_type ) is used inside the loop: const some_type & amp; = Some_object-> Get_some_type (); A large container for returning // by the referee (...) {...; Data_type = some_type.at (...); ...; }

I think that if we are obliged to use the returns by the above values, then I need to start a new function get_some_type_at Get an element of the container from the value (should there be any downsides to do this, should I know, in the display?):

 for  (...) {... ; Data_type = some_object- & gt; Get_some_type_at (...); // Returns the value of a small item ...; }   

Then I'm mostly looking for confirmation of these aspects but details and insights are most appreciated. Thanks for your time and interest

The best compilers return value optimization Apply. , So you do not have to worry about the unnecessary creation of copy objects.

Therefore, select

  some_type get_some_type (...) {...; Return ...; }   

Unless you are using an old compiler.

(Acknowledgment for juanchopanza; also drop the const object returns, explanations in the comments.)

For more information see; It is redundant to duplicate here.

No comments:

Post a Comment