Re: reference to local variable

Tech-Archive recommends: Fix windows errors by optimizing your registry



I think you are confusing the compiler with a lint tool. The compiler
is only required to report errors, not warnings. The warnings are just
an icing. True, we expect a lot of icing from the compiler, but still
that's all it is. If you need deep code analysis you should use lint.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Alan Carre" <alan@xxxxxxxxxxxxxxxxx> wrote in message
news:uZO9ioiTIHA.3916@xxxxxxxxxxxxxxxxxxxxxxx

"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
wrote in message news:uKhQE6hTIHA.1168@xxxxxxxxxxxxxxxxxxxxxxx
Alan Carre wrote:
In this case, I think the compiler should be able to determine that
&(s.i) is an address of a local variable and should issue the warning:

warning C4172: returning address of local variable or temporary

I would definitely say that this is a bug in the compiler.

I'd call it the lack of a feature. There's nothing in the standard or
elsewhere that stipulates the constructs for which a compiler should
generate warnings or errors. In this case, the VC++ team appears to have
implemented a warning for the easy case, which probably covers 90% of the
real-world situations.

Failure to generate *some* warnings can very-often be labeled "compiler
bugs". I wouldn't expect the compiler to catch everything but there are
definite cases (such as this one) where an expected warning can and should
be issued.

For instance, here's another one. Even at warning level 4 the following
code does not generate any warnings:

int j=4;
bool b = j = 7 ? true : false;

The programmer's intention is quite clear, he/she meant to test whether or
not j is equal to 7. Instead j gets assigned the value 1 and b becomes
"true".

Of course nobody should ever write code this way (ie. leaving out brackets
in conditional :? expresions), but the compiler can easily bring our
attention to this error by issuing a "warning: conditional expression is
constant". I mean, if I've turn that warning on why doesn't it tell me
when conditional expressions are constant? In a large project one requires
that the compiler do as it's told so we can trust that a "warningless
compile" contains none of the specific expression types we asked the
compiler to alert us to.

Side note: if we always remember to put in brackets when using :? we will
catch this one via. "warning: assignment in conditional"!

I'm not sure if anyone tried, but I'd try compiling this code with /W4
and /O1. Many more warnings are diagnosed in an optimized build (since
they're produced by the optimizer).

I haven't, but I will certainly try it. Good suggestion.

- Alan Carre




.



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... using a Microsoft compiler. ... nilges.c: error C2001: newline in constant ... nilges.c: warning C4127: conditional expression is constant ...
    (comp.lang.c)
  • Re: Why INFINITE loop in a thread occupy so much CPU time??
    ... With the attendant warning about the constant expression. ... This is a common programming technique. ... Good programming practice would consist of using the compiler at ... One can write 'bool' in C++ but not C. ...
    (microsoft.public.vc.mfc)
  • Re: help needed please!
    ... But some compilers like to give you a warning ... Each warning a compiler can give you is important when you not ... but you may use flages to handle flags to handle flags when you ... >> stdout is line bufferd and until one or both of the following cases is ...
    (comp.lang.c)
  • Re: Overloading abstract methods
    ... I'm calling this abstract class ResumeWriter. ... > use of in each subclass is up to you. ... >> However, if I do that, I get compiler warnings that the parameters for ... Disable that warning. ...
    (comp.lang.java.programmer)
  • Re: Why does this work?
    ... The compiler is able to tell the difference between the two by the context in which the word "color" is used. ... The point is that the compiler reads the original code as attempting to access a shared member of an instance. ... That's why the compiler is warning the programmer that it's not going to do what was coded. ...
    (microsoft.public.dotnet.languages.vb)