August 23, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

returning a reference instead of a pointer

 

Ok, this could be a trivial question, but I’m becoming confused now. Imagine a write an instance method as the following:

  1. QString aMethod(){
  2.     return QString( "Hello World" );
  3. }

such method builds an object on its stack, and then returns it. The caller can use the result as a reference (or at least assign it later to a reference). But is this correct? I mean, shouldn’t the object be destructed as the method stack frame is released?
Moreover, it will be better and or possible to return a QString& (I found only examples on scalar reference returning).
If someone can help me understand and/or point to a good description of the problem it will be great!

Thanks

6 replies

August 23, 2011

mlong mlong
Mad Scientist
1517 posts

The code you posted creates a QString on the stack, and returns a copy of it. If a reference were returned, it would be invalid because the original QString would have gone out of scope at the end of the aMethod() call.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 23, 2011

Andre Andre
Area 51 Engineer
6031 posts

Note that any decent compiler will optimize the copy away, though, so it is not as bad as it sounds.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

August 23, 2011

octal octal
Lab Rat
74 posts

Moreover, in addition of what mlong and Andre said, remember that Qt uses « Implicit Sharing » as an « optimization », which means that when a QString is copied, it only involves a pointer copy (pointer to some Data) and incremation of an atomic refcounter. This isn’t free but it still does the job.

However, in such a case, decent compilers should optimize and avoid the copy, as Andre said. This is so called « Return Value Optimization (RVO) », see here [en.wikipedia.org]

Doing this :

  1. QString& aMethod(){
  2.     return QString( "Hello World" );
  3. }

is dangerous and will lead to an Undefined Behavior, since the QString on the stack will be destroyed at the end of the scope and thus the reference would become invalid (as stated by mlong).

  1. QString *aMethod(){
  2.     return new QString( "Hello World" );
  3. }

This, is as bad as the previous example, since you’ll have to explicitely remember to delete the memory if you don’t wanna risk to leak memory (or you’ll have to encapsulate the pointer into a Smart Pointer Class).

One thing to remember is that premature optimization is evil. So, let the compiler optimize such operations instead of going into an undefined behavior :) .

November 10, 2011

ardhitama ardhitama
Lab Rat
7 posts

If you already familiar with c++11 then that r-value passing will be faster if QString had move semantics implemented.

 Signature 

Love automagically stuffs

November 16, 2011

raja26 raja26
Lab Rat
72 posts

As far as QString is concerned you don’t have to worry about this, because QString is not copied, as octal said and I too remember(?) of reading something like this.

November 16, 2011

Andre Andre
Area 51 Engineer
6031 posts

Copying a QString is indeed a fast operation, just copying a pointer and incrementing a counter. However, not copying is going to be even faster, of course.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ [SOLVED] QGLWidget::makeCurrent () is per widget only or per thread?      [SOLVED] Implementing C library in Qt GUI ››

You must log in to post a reply. Not a member yet? Register here!