Re: Limit on maximum number of register variable in C program

From: Dave Vandervies (dj3vande_at_csclub.uwaterloo.ca)
Date: 02/25/05


Date: Fri, 25 Feb 2005 18:46:13 +0000 (UTC)


In article <athu11lr1ubpqm4n6nmc00tsfrc7ndcg4l@4ax.com>,
Alan Balmer <albalmer@spamcop.net> wrote:
>On Thu, 24 Feb 2005 04:16:16 +0000 (UTC), dj3vande@csclub.uwaterloo.ca
>(Dave Vandervies) wrote:
>
>>In article <s84q11hiq3f9ef4qmtv7as0gvvsiu2n7ia@4ax.com>,
>>Alan Balmer <albalmer@spamcop.net> wrote:

[drifting over to const from a discussion of ignoring register]

>>>Yechh. I have to work with a (HP-UX) compiler which issues a warning
>>>every time it sees "const", whether or not it's used properly.
>>
>>This is much less unreasonable for register than it is for const, just
>>because of the way the two are used.
>>
>>But even for const... would you prefer that the compiler warned every
>>time the code modified an object, because that object could have been
>>declared const and the compiler knew it ignored the const if there was
>>one and might therefore have missed a constraint violation?
>>
>I read the above twice, and apparently fail to parse it correctly.
>Anyway, I would prefer that the compiler complain if constness is
>violated, not otherwise. The warning I'm talking about is to the
>effect that "const objects may be stored in read-only memory, and
>attempts to modify them may fail." Regardless of whether any such
>attempt was made. The warning has no relation to the code - it's just
>spitting out a line from the programming manual.

It sounds to me like that's the compiler's way of saying "I don't do
semantics checking on const, so I might be missing a constraint violation,
so here's a diagnostic just in case", though somewhat less so now that
I've seen the actual text of the warning. (Unless it also complains
about code that actually does violate the constraints on const objects?)

Doing the equivalent with register, as I was suggesting (now snipped), is
actually a sensible thing to do for a completely-and-correctly implemented
compiler - any self-respecting optimizing compiler will ignore it during
code generation anyways, and the constraints on register objects don't
really serve any useful purpose in correctness checking.

For const, doing this means that the compiler implementor was either
too lazy or (more likely) too rushed to properly implement const (I
suspect by adding it to a pre-ANSI compiler?). Not a Good Thing, but
not entirely unreasonable either.

So my question was, would you prefer this behavior (warning when it
sees a keyword it ignores) over the behavior Ben suggested (warning
when a constraint would be violated if the object being acted on had
the storage class defined by the keyword that was being ignored)?

--------
void foo(void)
{
    int a;

    /*I was talking about a warning here
      ("register keyword ignored")
    */
    register int b;

    /*Ben was talking about a warning here
      ("register keyword may have been ignored in declaration of operand of &")
    */
    do_something_with(&a);

    /*This is a constraint violation; either warning above will give
        the required diagnostic when this happens as well as in cases
        where it doesn't
    */
    do_something_with(&b);
}
--------

Note especially that I'm not claiming either behavior is "good" for
const, just that one is more reasonable than the other, for the same
reason it's more reasonable for register.

dave

-- 
Dave Vandervies                                 dj3vande@csclub.uwaterloo.ca
What you have in mind about trees is extremely obscure.  They come in green,
red-black, avl, binary, B, deciduous, non-deciduous, tropical, fruit,
ornamental, just to name a few varieties.        --CBFalconer in comp.lang.c


Relevant Pages

  • Re: compilation warning with const parameter
    ... into a const pointer to that element, ... The warning is thus correct: ... Compilers are required to produce diagnostics if constraints ... A compiler is not required to stop compiling upon detecting something ...
    (comp.lang.c)
  • Re: List<> of struct with property. Cannot change value of property. why?
    ... temporary), the compiler could complain and say: ... Well, IMHO the compiler could easily provide the same warning today, without the "const" keyword. ... Relying on the "const" keyword to enable a warning wouldn't have been a good idea in C++, because you'd get a lot of false positives due to the large amount of code that is "const" without using "const". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Simple custom filter crash (AddRef()) on Windows Mobile 5.0
    ... I am using "regsvrce.exe" to register the filter. ... const AMOVIESETUP_MEDIATYPE sudPinTypes = ... CBaseRenderer, pUnk, phr) ...
    (microsoft.public.windowsce.app.development)
  • Re: Is const really const?
    ... I have a doubt in const keyword. ... you may be using a compiler which is fully ANSI compliant. ... const1.c:4: warning: initialization discards qualifiers from pointer ... promise,by not allowing programmer modify the const variable directly (p++ ...
    (comp.lang.c)
  • Re: Is const really const?
    ... declared const can not be modified by the module it is defined in. ... For one compiler, it produces a warning ... Once the compiler diagnoses the problem, though, it's allowed ...
    (comp.lang.c)