3 ms·
>I would hazard a guess that your coworkers would struggle in pretty much any language? Uh? This issue wouldn't happen in Java because everything is heap(GC) a
by renox 6y ago
>I would hazard a guess that your coworkers would struggle in pretty much any language?
Uh? This issue wouldn't happen in Java because everything is heap(GC) allocated.
Which is probably why developers new to C++ have this issue.
Walter Bright said below that the D compiler find most of these issue at compile time, well that's nice for D but unfortunately that isn't the case for C++ at least not for gcc9.
- WalterBright 6y agoDoing what D does to detect references to expired stack frames would require some restructuring of C++'s semantics, which seems unlikely. For example, D can detect this error: @safe: int* f(int* p) { return p; } int* g() { int i; return f(&i); } Compile with: dmd test -dip1000 and the result: Error: reference to local variable i assigned to non-scope parameter p calling test.f
- scatters 6y agoAt -O2 or above, gcc emits Wreturn-local-addr.
- mlyle 6y agoWithin a single compilation unit, and only in this most trivial case.
- WalterBright 6y agoHere's an example of a much more complex case that is detected: https://issues.dlang.org/show_bug.cgi?id=21745 https://issues.dlang.org/show_bug.cgi?id=21745
- mlyle 6y agoYes, sorry if my comment isn't clear. D is very capable in this regard. While gcc with C/C++ code will spot a few trivial cases... including the one in the example above.
- renox 6y agoYep, ‘-O2 or above’ we used to compile our UTs without optimizations because otherwise it takes too long, but now we have an additional(less frequent) CI job for UT with optimizations to benefit from these improved error detection.