Topic: Theory & implementation of const int


Author: James Kuyper <kuyper@wizard.net>
Date: 1998/01/20
Raw View
Harvey Taylor wrote:
...
>     I see where Stroustrup says in C++ Programming Language (3rd Edition)
>     Section 5.4, "if the compiler knows every use of the const, it need
>     not allocate space to hold it."
>
>     Consider the two methods:
>         const int       Max_Eggs        32;
>
>         #define         MAX_EGGS        32
>
>     Is it a requirement of the DIS standard that the line declaring
>     Max_Eggs not allocate memory?

No, but neither does it require that MAX_EGGS not allocate memory. In
either case, it is a quality of implementation issue. It's simply easier
to identify the fact that MAX_EGGS doesn't need storage.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: het@pangea.ca (Harvey Taylor)
Date: 1998/01/14
Raw View
Hi Folks,
    I have a two part question about using const int rather than
    #define.  I understand more or less the theory. However I
    wonder about actual compilers today.  I am working in a
    realtime, embedded environment and I need to know what
    memory is being allocated.

    I see where Stroustrup says in C++ Programming Language (3rd Edition)
    Section 5.4, "if the compiler knows every use of the const, it need
    not allocate space to hold it."

    Consider the two methods:
 const int Max_Eggs 32;

 #define  MAX_EGGS 32

    Is it a requirement of the DIS standard that the line declaring
    Max_Eggs not allocate memory?

    Is it discretionary for the compiler implementor?

    Can the compiler actually tell in a foolproof manner whether I
    have or not messed with some pointer, cast away the const and
    modified Max_Eggs?

    Finally, what do actual compilers currently do?  I am working
    with GNU 2.7.2, if anyone happens to know its behaviour.

<curious>
-het

  "Ah, but you do not understand. To be on the wire is life.
 And the rest is waiting."  - Papa Wallenda

        Harvey Taylor       Internet: het@despam.pangea.ca
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/01/14
Raw View
Harvey Taylor <het@pangea.ca> wrote:
:     Consider the two methods:
:  const int Max_Eggs 32;
:
:  #define  MAX_EGGS 32

:     Is it a requirement of the DIS standard that the line declaring
:     Max_Eggs not allocate memory?

No.

:     Is it discretionary for the compiler implementor?

Yes.

:     Can the compiler actually tell in a foolproof manner whether I
:     have or not messed with some pointer, cast away the const and
:     modified Max_Eggs?

They don;t have to, since what you describe is an undefined
behavior anyway. However, there are other scenarios where
a compiler might be forced to allocate memory for the constant.

:     Finally, what do actual compilers currently do?  I am working
:     with GNU 2.7.2, if anyone happens to know its behaviour.

GNU compiler never allocated memory for as long as I can remember.
I guess most compilers do the same nowdays.

If you want to enforce no-storage, declare your variable to be
a value of an unnamed enum:

enum { Max_Eggs = 32 };

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/01/16
Raw View
Harvey Taylor wrote:
>
> Hi Folks,
>     I have a two part question about using const int rather than
>     #define.  I understand more or less the theory. However I
>     wonder about actual compilers today.  I am working in a
>     realtime, embedded environment and I need to know what
>     memory is being allocated.
>
>     I see where Stroustrup says in C++ Programming Language (3rd Edition)
>     Section 5.4, "if the compiler knows every use of the const, it need
>     not allocate space to hold it."
>
>     Consider the two methods:
>         const int       Max_Eggs        32;
>
>         #define         MAX_EGGS        32
>
>     Is it a requirement of the DIS standard that the line declaring
>     Max_Eggs not allocate memory?

AFAIK no, nor is it a requirement not to allocate it more than once,
as long as it can ensure you won't notice it (that is, your program
will not behave differently - wasted resources don't count here).
But why should an implementor do this? I guess, he wouldn't be too
successful with such a compiler... (Non-optimized compiling is a
different issue, of course, as leaving the constant out _is_ indeed
an optimization).
However, if you have an initializer which can't be computed at
compile time, the compiler cannot avoid allocating the value (but then
a preprocessor macro wouldn't do the right thing anyway).

>
>     Is it discretionary for the compiler implementor?
>
>     Can the compiler actually tell in a foolproof manner whether I
>     have or not messed with some pointer, cast away the const and
>     modified Max_Eggs?
>

As long as you don't add extern, the scope of the const is limited
to the current translation unit. That is, the compiler sees every use
of it, and if it doesn't see an operator& for or reference to it,
there's no reason to allocate memory (the situation is different
with more complex objects, of course). Casting away the const is
undefined behaviour, and the compiler is free to ignore all changes
which are done to it, or core dump as soon as you try to change it.

>     Finally, what do actual compilers currently do?  I am working
>     with GNU 2.7.2, if anyone happens to know its behaviour.
>

I did a small test, with the following result:

Without any optimization, it generates the variable, but already
with -O1 neither nm, nor gdb is able to find the memory.
However, passing the constant per const reference makes g++ allocate
the memory even with optimization on. However, passing simple objects
like ints by reference should not be too common (it might appear in
template specialisations, however).

Of course I wrote a rather simple program, but as it should not be
difficult to have this behaviour (just don't emit the variable
until you see an operation which needs it), I think it should be
this way even with larger programs.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]