Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++ trivia

  • You cannot reply to this topic
10 replies to this topic

#1 swiftcoder   Senior Moderators   -  Reputation: 10364

Like
0Likes
Like

Posted 21 January 2014 - 09:17 AM

Everyone knows that taking a reference to a temporary is a bad idea:
int getX() {
    return 5;
}

int &x = getX();
But how about a const reference?
int getY() {
    return 10;
}

const int &y = getY();
It turns out that unlike the first case, storing a const reference to a temporary actually extends the lifetime of the temporary to lifetime of the const reference (read Herb Sutter on the topic).
 
Entertainingly enough, I learned this piece of C++ trivia when Coverity's static analysis tool tried to tell me that returning a const reference to a temporary was illegal. All I can say is yay for code checking tools that don't understand the C++ standard...

Edited by swiftcoder, 21 January 2014 - 10:04 AM.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9668

Like
5Likes
Like

Posted 21 January 2014 - 09:45 AM

y in getY() isn't a temporary. Returning a const reference to it won't extend it's lifetime.



#3 swiftcoder   Senior Moderators   -  Reputation: 10364

Like
0Likes
Like

Posted 21 January 2014 - 10:05 AM

y in getY() isn't a temporary. Returning a const reference to it won't extend it's lifetime.

You are entirely right. I wasn't thinking when I wrote up that example.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#4 SiCrane   Moderators   -  Reputation: 9668

Like
0Likes
Like

Posted 21 January 2014 - 11:26 AM

Entertainingly enough, I learned this piece of C++ trivia when Coverity's static analysis tool tried to tell me that returning a const reference to a temporary was illegal. All I can say is yay for code checking tools that don't understand the C++ standard...

Incidentally, I'm inclined to side with Coverity here. I can't say for certain unless I saw the code it was actually complaining about, but the standard specifically states that a temporary bound to the returned value in a function only persists until the function exits (12.2 paragraph 5 of C++98 and C++11, though C++11 makes the wording much more clear: "The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not extended; the temporary is destroyed at the end of the full-expression in the return statement.").

#5 swiftcoder   Senior Moderators   -  Reputation: 10364

Like
0Likes
Like

Posted 21 January 2014 - 01:45 PM

I can't say for certain unless I saw the code it was actually complaining about

Suitably obfuscated, but I think all the relevant information is preserved:
class X {
public:
    X(int i);
};

class Y {
    X mX;
    bool valid;

    const X & test() {
        if (!valid) {
            return X(0); // coverity reports illegal return of reference to temporary
        }
        return mX;
    }
};

but the standard specifically states that a temporary bound to the returned value in a function only persists until the function exits

Will that not in practice result in copy construction of a new temporary in the outer scope?

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#6 SiCrane   Moderators   -  Reputation: 9668

Like
0Likes
Like

Posted 21 January 2014 - 02:52 PM

Yeah, I'm pretty sure that Coverity is right on this one. The return X(0) creates a temporary that is destroyed at the end of the full expression of the return statement. The const reference in the outer scope will not create a new temporary because it isn't being initialized with an object, it's being initialized with another const reference. Even if it did know it needed to copy construct something, the end of a function is a sequence point and the object it would try to copy would already been destroyed.



#7 swiftcoder   Senior Moderators   -  Reputation: 10364

Like
0Likes
Like

Posted 21 January 2014 - 03:17 PM

Interesting. Running the following with clang (previous work was with GCC):
 
#include <iostream>
 
class X {
  int mX;
  public:
    X(int i) : mX(i) { std::cout << "constructor" << std::endl; }
    ~X() { std::cout << "destructor" << std::endl; }
    X(const X &x) : mX(x.mX) { std::cout << "copy constructor" << std::endl; }
 
    int get() const { return mX; }
};
 
const X &getX() {
  std::cout << "in getX" << std::endl;
 
  return X(-123);
}
 
int main() {
  std::cout << "entering main" << std::endl;
 
  const X &x = getX();
 
  std::cout << "x: " << x.get() << std::endl;
 
  std::cout << "exiting main" << std::endl;
}
 
Results in the following output:
$ ./a.out 
entering main
in getX
constructor
destructor
x: -123
exiting main
Note that the destructor is in fact called when the getX() function exits, and before I attempt to access the member variable (which succeeds, I assume incidentally).

Edited by swiftcoder, 21 January 2014 - 03:18 PM.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#8 Zaoshi Kaba   Crossbones+   -  Reputation: 4567

Like
0Likes
Like

Posted 21 January 2014 - 03:29 PM

Sounds right, you're basically returning a pointer. I get same result in VS2013 Express. However changing signature to const X getX() gives different result:

entering main
in getX
constructor
x: -123
exiting main
destructor

So it kinda did extend object's lifetime, just as OP said.


Edited by Zaoshi Kaba, 21 January 2014 - 03:29 PM.


#9 SeanMiddleditch   Members   -  Reputation: 7144

Like
1Likes
Like

Posted 21 January 2014 - 03:58 PM

"Works" does not mean it's correct. It just means that the result of your undefined behavior happened to coincide with your expectations. Recompile, change compilers, change optimizations, etc. may all change the result.
 

const X &getX() {
  std::cout << "in getX" << std::endl;
 
  return X(-123);
}


This is illegal and undefined behavior. Period. That it works in some test means nothing other than that you got "lucky" (or more accurately, you got unlucky and got fooled into thinking the code is correct when it very much is not).

The reference is (under the hood) a pointer on the vast majority of C++ implementations. Ask yourself what it would be pointing to. In this case, it'll be pointing to a local variable which on most real-world machines means it's pointing to a memory address on the stack inside of getX(). Returning from the function doesn't destroy the stack; it just means that the address is now unused and the memory is now allowed to be reused by something else later on. If it appears to work, that's just because that address on the stack hasn't _yet_ been reused. You can likely find out how broken this code is by storing that reference, calling some other functions, and _then_ dereferencing the value:
 
// grab reference to stack value that is no longer "live"
const X& tmp = GetX();

// reuse the stack space for other calculations
some_other_non_trivial_function();

// dereference the tmp address and try to copy a value
// into the stack or a register (depends on calling
// conventions) for consumption by operator<<(ostream&, int)
std::cout << tmp.get();
Depending on optimizations and a bunch of other factors this is much more likely to print out total garbage (or even crash) when you write out the value of tmp as the address referenced by tmp will likely have been overwritten with something else.

You (and every C/C++ programmer) should read these slides on "Deep C" by Maudal and Jagger and fully internalize the information they present:
http://www.slideshare.net/olvemaudal/deep-c

I don't care if you have 20 years of experience and think you're the most bad-ass programmer around. Read those slides anyway. I lost count of how many multi-decade "veteran" C/C++ engineers I've worked with over the years that didn't know half of what those slides go over.

#10 frob   Moderators   -  Reputation: 22714

Like
5Likes
Like

Posted 22 January 2014 - 01:57 AM

You (and every C/C++ programmer) should read these slides on "Deep C" by Maudal and Jagger and fully internalize the information they present:http://www.slideshare.net/olvemaudal/deep-c

I don't care if you have 20 years of experience and think you're the most bad-ass programmer around. Read those slides anyway. I lost count of how many multi-decade "veteran" C/C++ engineers I've worked with over the years that didn't know half of what those slides go over.

Those slides really bugged me when I first saw them a while back.

Not because they were wrong or things I didn't know, but because they explicitly said something in the introduction: "Suppose you are about to interview a candidate as a C programmer for various embedded platforms."

As someone who actually spent several years doing exactly that, I can testify that such compilers are notorious for not following the language standard, that they have incomplete support for many language features, and the comparisons against C++ compilers is absurd because much embedded software only has C-ish compilers, not C++ compilers. Sure many newer bits of hardware have C++-ish compilers, few are trustworthy..

The slides are informative for some people, but they directly violate the premise given at the very beginning. When they start talking about what would happen in various C++ flavors it doesn't match the premise. I'd remind the candidate that this is a C programming job, so please keep it to the various C standards.

(Nothing in the slides really surprised me as I started learning C in '83, C++ in '88, and have read and re-read all of the C and C++ language standards. I have surprised many co-workers by opening up the official paid-for PDFs and answering questions with a line from the standards, in addition to finding and reporting fun compiler bugs over the years where I can cite the standard when asking for clarification on the issue. That is always fun.)

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#11 Paradigm Shifter   Crossbones+   -  Reputation: 5433

Like
0Likes
Like

Posted 22 January 2014 - 03:02 PM

Also, knowing the probable effects of undefined behaviour is no substitute for REMOVING the undefined behaviour from the code. In a real interview I'd be saying "don't write code like that, this is what you should do instead...".


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley





PARTNERS