-8

In my previous question (Why value captured by reference in lambda is broken?) answer is "because you access memory of variable that is out of scope".

In most common compilers (I mean Visual C++, gcc and clang) is there compiler warnings about this incorrect memory access?

Community
  • 1
  • 1
vladon
  • 8,158
  • 2
  • 47
  • 91
  • @AlanStokes Tried what? Visual C++ and gcc does not warn about it with default settings. Don't tried clang yet. – vladon Sep 14 '15 at 19:36
  • 1
    In general, compilers are not that good at giving that kind of hints, as this is purely a run time problem (which the _compiler_ does not even see). In some cases, in can detect them. Sometimes you'll get warned, sometimes the compiler uses this information to drastically optimize your code. And sometimes, the compiler won't notice and you'll be in big trouble. – stefan Sep 14 '15 at 19:38
  • @AlanStokes I mean is there compiler command-line switch to enable it? May be somebody knows. – vladon Sep 14 '15 at 19:39

1 Answers1

2

Generally? No. It's intractible to do so. They can't.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Here come the downvoters with their magic all-purpose compile-time dangling reference detector. – Lightness Races in Orbit Sep 14 '15 at 19:43
  • @LightnessRacesinOrbit Well, to be fair, it would have been nice to point for SCA tools available. I found that C.... (wait, no dont spell out, no advertising of commercial products) detected stuff with a reasonable false positive ratio. – πάντα ῥεῖ Sep 14 '15 at 20:21
  • @πάνταῥεῖ: The question does not ask for tools. If it did, it would be off-topic. – Lightness Races in Orbit Sep 14 '15 at 20:22
  • 1
    _@LightnessRacesinOrbit_ Of course, but you stated _it's intractible_, which isn't completely true, and proven by some useful tools. I would have added such point in an answer of mine. Not I'm anyhow agreeing with @stefan's attitude about you. – πάντα ῥεῖ Sep 14 '15 at 20:26
  • @πάνταῥεῖ: It's intractible in general as far as I'm aware, but I'm certainly open to the possibility of the existence of such tools (which I don't use) proving me wrong! – Lightness Races in Orbit Sep 14 '15 at 20:27
  • 1
    @LightnessRacesinOrbit Yes for the _general case_ you're absolutely right (at compile time stage of course). But in fact there's a number of static code analysis tools (including non commercial), that do a fairly good detection of possible out of bound reads, or otherwise undefined behaviour detection. We've been recently switching from K... to C..., because of the better false positive ratio, and track down features. It still needs human introspection and triage, but is fairly helpful to detect such kind of errors. – πάντα ῥεῖ Sep 14 '15 at 20:37
  • @πάνταῥεῖ: That's why the first word of my answer is "Generally"! I don't know what the OP's specific use case is, unless he intends for the linked question to be it. I don't use static analysis much. Have been meaning to look into it more, but never could be all that bothered frankly! – Lightness Races in Orbit Sep 14 '15 at 20:39
  • 2
    Everyone, let's stop with the personal attacks in the comments. I've cleaned this up, but let's keep things focused on the technical issues here. – Brad Larson Sep 14 '15 at 20:49