Inspired by Herb Sutter, a convincing lecture Not your father C ++ , I decided to look again at the latest version of C ++ using Microsoft Visual Studio 2010. I was especially interested in Herb's statement that C ++ is “safe” because I did not hear as C ++ 11 resolved the well-known upstream loss problem. From what I can say, C ++ 11 does nothing to solve this problem and, therefore, is not “safe”.
You do not want to return a link to a local variable, because the locale is allocated on the stack stack, which will no longer exist after the function returns, and therefore the function will return a dangling pointer to the allocated memory, which will lead to deterministic data corruption. C and C ++ compilers know this and warn you if you try to return a link or pointer to a local one. For example, this program:
int &bar() {
int n=0;
return n;
}
causes Visual Studio 2010 to issue a warning:
warning C4172: returning address of local variable or temporary
However, lambdas in C ++ 11 makes it easy to capture a local variable by reference and return that link, resulting in an equivalent dangling pointer. Consider the following function foo, which returns a lambda function that captures a local variable nand returns it:
#include <functional>
std::function<int()> foo(int n) {
return [&](){return n;};
}
- . :
1825836376
, Visual Studio 2010 .
. , . , , (, "" ++ StackOverflow ). ? - ?