Re: where do declare a variable?



Joseph M. Newcomer wrote:
:: See below...
:: On Thu, 22 Nov 2007 13:06:54 +0100, "Bo Persson" <bop@xxxxxx>
:: wrote:
::
::: Vaclav wrote:
::::: In fact, I started to think about this when LINT suggested (for
::::: sample 1) that QLin* pLin should be declared as const. Can that
::::: make a difference (adding const) in the code generated by the
::::: compiler?
:::
::: It doesn't affect the code in any way.
:: ***
:: Actually, this is not true. const can have a profound effect on
:: code quality given the right circumstances. (a) why do you think
:: it has no effect? (b) under what conditions did you determine this?
::
:: As someone who regularly took advantage of const declarations in
:: writing compiler code, I can say that it is a good practice for
:: something that is not modified. ****

Sure, it is a good practise. I use it all the time. :-)

However, the compiler knows if a variable is modified or not. That's
how it can tell me if I try to assign a new value to something
declared const.

:::
::: It does affect the compiler's ability to tell you when you are
::: accidentally changing the value. If it is supposed to stay the
::: same all the time, adding a const shows that intention.
:: ****
:: It affects common subexpression computations, code flow (both
:: alpha and omega flows), and value propagation.
:: ****

But it doesn't matter if I tell the compiler or not. I has to check
this for itself anyway.


Bo Persson


.



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... and make all strings const save where the intent ... function whose contract is to change the string. ... the compiler "just" prevents the string ... try to do using the pointer you get. ...
    (comp.lang.c)
  • Re: Writing single bits to a file
    ... The 'const' keyword provides the same kind of benefit that prototypes ... enabling the compiler to warn you if it detects the fact that you ... I had not gone and more correctly fixed up the precedence heirarchy (unary ... I like to implement scripting languages for breakfast ...
    (comp.lang.c)
  • Re: const
    ... > const modifiers, ... willing to remove the const'ness with a cast, ... I don't know how good a cross-file compiler could be - it would have to be ... it's a guarantee by the programmer. ...
    (comp.programming)
  • Re: Setting PropertySheet Title (Wizard mode)
    ... If you think of it as optimization, it is probably the wrong reason. ... 'const' captures the purpose of the interface. ... I was trying to use pass-by-value to mean situations where the value ... As an old compiler writer, ...
    (microsoft.public.vc.mfc)
  • Re: Index a #define string
    ... In this case, i wouldn't use an array myself but a const char * const, but ... should one make it of static storage? ... If they were on the stack, where do you think their values ... You mean that the compiler optimises it out. ...
    (comp.lang.cpp)