|
|
|
Ask HN: Why can't C++ compilers find this error?
|
|
1 points
by greggman7
520 days ago
|
|
Debugging some 3rd party library I ran into a "reference a stack object that no longer exists" error. repo #include <iostream>
int* f() { int x = 4; int* xp = &x; return xp; }
int main() {
int* p = f();
std::cout << *p;
}
Without giving it much thought, why is this hard for a modern C++ compiler to detect?Note that gcc will detect some of these #include <iostream>
int main() {
int* p;
{ int j = 123; p = &j; }
std::cout << *p;
}
Gets an error in gcc (not in clang)I know this type of issue is a strong reason people prefer rust but I'm still surprised a modern C++ compile doesn't find these issues. Is it an intractable issue? |
|
It is hard in the general case because none of the operations individually are a problem: "create a variable on the stack", "create a pointer to a variable on the stack", and "return a pointer" are all valid operations. It's the last two together that become a problem. And there's no mechanism in C++ to connect the two together, so that this case can be recognized and protected against. Compilers can try, and see some obvious cases, like you've noticed, but it's going to be a conservative analysis at best.
> Is it an intractable issue?
In some sense, yes, in others, no. Solving it correctly in 100% of cases is not going to happen. But they're working on some things that will hopefully catch most cases. We are supposed to get some more news on that soon, we'll see.