-3

The book says:

enter image description here enter image description here

But I write a program with function clone2

It works without any error.

Is what book says still correct?

Thanks

Stats Cruncher
  • 155
  • 1
  • 1
  • 6
  • The book is *mostly* correct, but `newguy` is not a temporary variable. It's a local variable whose lifetime is ending. – Brian Bi Jul 25 '16 at 07:40
  • 1
    Does the book actually mention undefined behavior in that chapter? If not, well.... – LogicStuff Jul 25 '16 at 07:41
  • When a variable ceases to exist, the memory still exists, probably with the same value, but it is memory that is "marked" as free, so, the behaviour is undefined. Many programs could works many times with errors like this, but, anytime, it stops working because this free memory changes. – EFenix Jul 25 '16 at 07:42
  • I add the next page. The book does not mention it is undefined behavior just says it would be a problem. – Stats Cruncher Jul 25 '16 at 07:55
  • Re-opened as the duplicate was about returning a non-`const` reference to a variable with automatic storage duration. – Bathsheba Jul 25 '16 at 08:21
  • undefined behavior means anything can happen, including it appears to work – phuclv Jul 25 '16 at 08:22
  • Here is the warning message from MVS:"Warning C4172 returning address of local variable or temporary: newguy" – Stats Cruncher Jul 25 '16 at 09:50

2 Answers2

2

The book is correct, although the use of the term temporary variable for a variable with automatic storage duration only adds to the confusion. Furthermore the book appears to omit the fact that that the program behaviour is undefined.

Hence your compiler is allowed to do anything, including giving the impression that what you're doing is legal.

Short answer: don't do it.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • That was my initial thought but I don't think lifetime extension applies here as the extension is not *transitive*. – Bathsheba Jul 25 '16 at 07:44
  • 2
    @cplusplusrat Lifetime extension does not apply within a `return` statement. In effect, an object cannot survive beyond its scope; at most a temporary can survive until the end of the scope in which it is created. – Brian Bi Jul 25 '16 at 07:45
  • Thank you for your answer. I use Microsoft visual studio. When I saw it works, It worried me a lot. Now I notice MVS gives a warning. – Stats Cruncher Jul 25 '16 at 07:47
  • @Brian the got link I gave gives an example of the return type. Is the difference in the two because, the got example returns an rvalue and the example the the OP returns an lvalue? – cplusplusrat Jul 25 '16 at 07:51
  • 1
    @cplusplusrat That is not extending the lifetime of any variable inside the function. It's extending the lifetime of the temporary variable that holds the return value, created in the caller's scope. – Brian Bi Jul 25 '16 at 07:53
  • @Brian Just to make sure I understand this. What you are saying is a function with signature `const std::string& f()` is legal if the body is `return "abc"` and illegal if the body is `std::string foo("abc"); return foo;`? Assume I am calling this using `const std::string& val = f()` – cplusplusrat Jul 25 '16 at 08:01
  • @cplusplusrat both are illegal. They both returning reference to some [temporary] variable in inner scope. Lifetime extention happens in these cases: `std::string f(); /*...*/ const std::string& s = f();` ← `s` is bound to temporary variable returned by `f()` and extends its lifetime. – Revolver_Ocelot Jul 25 '16 at 08:18
  • @cplusplusrat *"What you are saying is a function with signature `const std::string& f()` is legal if the body is `return "abc"` "* That appears to be opposite of what Brian said. Here, the temporary is created within the function, and as Brian said: *"an object cannot survive beyond its scope"*. Your earlier question hits quite near the spot: The function in gotw returns a temporary object, not a reference, which is very important here. – eerorika Jul 25 '16 at 08:20
  • Aha, I had missed the function signature in the GOT example. Herb's `f()` returns `std::string` by value and not a reference. My apologies. I think I have always misunderstood this. I always thought that if a function returns a reference to a temporary, if bound with a const reference would have its life extended. This was clearly wrong. Deleted my original comment. – cplusplusrat Jul 25 '16 at 08:27
0

The local variable newguy will be created/allocated on the stack-memory. if you call another function with some local variables and assign values to them, they will overwrite the Memory of your newguy. So it might work, if you Access the variable after a call to clone2 - but you might not get the original Content of newguy, if you call some other function before accessing newguy. That's why it is not a good idea to do it ;-)

VVN
  • 1,607
  • 2
  • 16
  • 25