Which is an efficient way of memory to pass a string as an argument to a function.?

can someone tell me. Does it make sense to pass the string as func (string &) instead of func (string).

+4
source share
8 answers

Passing an object by reference means that you pass the link by value, instead of passing the object by value, which means you must make a copy when calling the function.

However, when passing by reference, a new set of questions is introduced, which you should know about:

  • Does the function change or not the passed object? If not, you must enter the const modifier
  • Do functions need to modify an object, but not subject changes outside the boundaries of a function? In this case, you really need a copy.
  • Does somewhere / somehow store the link of the passed object? In this case, you should understand how the property of the object is transferred and safely delete it. You can use smart pointers to solve these problems.

Point: just because passing by reference makes a function call cheaper, doesn't mean you should use it anyway.

+3
source

func(string &) passes the string by reference. This means that it will not be copied and that the function can change it.

func(string) passes a string by value. This means that a copy will be created and that the function can only change its own local copy of this line.

To pass a string without copying, but don't let it change, use func(const string&) .

If your function should accept a copy of the argument anyway, it is preferable to pass by value.

+3
source

Passing a link will usually be more efficient for complex types, but it should be a const reference if you don't want the function to be able to modify it:

 void f(string); // function gets its own copy - copying may be expensive void f(string&); // function can modify the caller string void f(string const&); // function can read the caller string, but not modify it 
+2
source

You can also use func(const string& ) instead of func(string& ) to make sure the pass-by-reference parameter does not erroneously change inside the function.

+1
source

When you pass it as a string , the string is copied first, and the copy is sent to the function. The best way to pass expensive objects, such as strings, if you do not want the function to modify them, is const string& .

Important const . First of all, it ensures that the string will not be changed inside the function. In addition, you cannot pass a literal to work without using func("this is a litearal because it isn't inside a variable") .

+1
source

Passing by reference is usually better than passing by value in terms of performance (excluding built-in data types). So yes func(string&) better than func(string) . Provided that the passed string can be changed when the function completes. If you are not going to change string , use func(const string&) .

On the other hand, I remember reading somewhere that string optimized in STL. If you pass string by value, this may not necessarily create a new string on the heap. Thus, transitioning by value may not be as expensive as you would expect. eg.

 string s1 = "hello"; // new buffer allocated on heap and copied "hello" string s2 = s1; // s2 and s1 both 'may' refer to same heap ... s1 += " world"; // s2 continue referring to old heap ... // new heap created to accomodate "hello world" referred by s1 
0
source

Yes. Link passing is always effective.
A string can be stored by value, and copying can take as long as O (n) (i.e., copying takes time proportional to the length of the string). It will also cause heap memory allocation, which is usually much more expensive than a copy.
You can also consider passing by reference to const, i.e. Func (const string &) to avoid any unintentional changes.

0
source

You can also try using rvalue link optimization with move semantics.

 void func(string &&s) { myS = std::move(s); } 
0
source

All Articles