Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fastest way to pass large objects

Take the following function as a example

string print()
{
   return (some string that has been formed);
};

and now lets say that this print function will form a ridiculously large string, that needs to get back to the function that called it from another class.

Now say this print function may be called thousands of times. Is there a correct and faster way to do this?

In Visual Studio you can do this

void print(string& lines)
{
   lines = (some string that has been formed);
};

however g++ will not compile this, nor should it be done this way as this could be a temporary object that could disappear at any second. So from what I understand is I could do this

void print(const string& lines)
{
};

if all I wanted was to retrieve something in this string w/o modifying it and do it very quickly. But I do want to modify it. So this I can not do.

Which leads me to the question is declaring a new pointer to a object going to be the fastest way to do this? or is it slower? or is it just not worth it. i.e. (not sure if this would even work?)

void print(string* lines)
{
   string formedString; // the string that has been formed
   lines = formedString&;
};

And finally, if I am loading object up at runtime, and adding them to a tree via:

void add(const ItemType& item)
{
   someNode->item = item;
};

The item was originally a temporarily stored object in the scope of the function calling this add(), so would assigning it to the node cause the stack of the calling function to stay around longer then wanted? Or would this be OK? Also would I later be able to edit item, or is it now stuck as a const?

Just questions of a learning mind :)

like image 388
WIllJBD Avatar asked Mar 08 '26 14:03

WIllJBD


1 Answers

Just return the string and be done with it.

The compiler will use return value optimization (or named return value optimization) to eliminate extra copies of the string as you return it. A really new compiler will also have a move constructor and move assignment operator for std::string, which will give even greater assurance of accomplishing the same thing.

Edit (sorry, I originally missed the last question): assigning a value will not preserve anything local to a function after that function returns. When a function returns, all its local variables will be destroyed. If you assign a value from that function to something that lasts after it returns, that's no problem -- it'll be copied (or maybe moved) to the destination. If you retain a pointer or reference to that local, it'll be dangling after the function returns, so trying to use the reference or dereference the pointer will lead to UB.

Depending on the situation, the latter may be a case where you want to use a shared_ptr, so both the function and the outside code will share access to the data, and the data will be destroyed only when both references to it are destroyed.

like image 169
Jerry Coffin Avatar answered Mar 11 '26 06:03

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!