static const variables in main()

Discussion in 'C++' started by Michael Klatt, Jun 16, 2004.

  1. Is there any practical difference between a local variable in main()
    declared 'const' and one declared 'static const'?

    int main()
    {
    static const int i1(0);
    const int i2(0);
    return 0;
    }
    Michael Klatt, Jun 16, 2004
    #1
    1. Advertising

  2. Michael Klatt wrote:
    > Is there any practical difference between a local variable in main()
    > declared 'const' and one declared 'static const'?
    >
    > int main()
    > {
    > static const int i1(0);
    > const int i2(0);
    > return 0;
    > }


    Not with int but for non POD types, the difference is when the
    destructor is called.
    Gianni Mariani, Jun 16, 2004
    #2
    1. Advertising

  3. Michael Klatt

    red floyd Guest

    Gianni Mariani wrote:

    > Michael Klatt wrote:
    >
    >> Is there any practical difference between a local variable in main()
    >> declared 'const' and one declared 'static const'?
    >>
    >> int main()
    >> {
    >> static const int i1(0);
    >> const int i2(0);
    >> return 0;
    >> }

    >
    >
    > Not with int but for non POD types, the difference is when the
    > destructor is called.


    Wouldn't it make a diff if main() was called recursively?
    red floyd, Jun 16, 2004
    #3
  4. * red floyd:
    > Gianni Mariani wrote:
    >
    > > Michael Klatt wrote:
    > >
    > >> Is there any practical difference between a local variable in main()
    > >> declared 'const' and one declared 'static const'?
    > >>
    > >> int main()
    > >> {
    > >> static const int i1(0);
    > >> const int i2(0);
    > >> return 0;
    > >> }

    > >
    > >
    > > Not with int but for non POD types, the difference is when the
    > > destructor is called.

    >
    > Wouldn't it make a diff if main() was called recursively?


    Not allowed.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 16, 2004
    #4
  5. Michael Klatt

    red floyd Guest

    Alf P. Steinbach wrote:
    > * red floyd:
    >
    >>Gianni Mariani wrote:
    >>
    >>
    >>>Michael Klatt wrote:
    >>>
    >>>
    >>>>Is there any practical difference between a local variable in main()
    >>>>declared 'const' and one declared 'static const'?
    >>>>
    >>>>int main()
    >>>>{
    >>>> static const int i1(0);
    >>>> const int i2(0);
    >>>> return 0;
    >>>>}
    >>>
    >>>
    >>>Not with int but for non POD types, the difference is when the
    >>>destructor is called.

    >>
    >>Wouldn't it make a diff if main() was called recursively?

    >
    >
    > Not allowed.
    >


    Thanks, Alf... couldn't remember if recursion into main was allowed
    (don't have a copy of the standard).
    red floyd, Jun 16, 2004
    #5
  6. Michael Klatt wrote:
    > Is there any practical difference between a local variable in main()
    > declared 'const' and one declared 'static const'?
    >
    > int main()
    > {
    > static const int i1(0);
    > const int i2(0);
    > return 0;
    > }


    In one of my compilers, there was a huge difference between
    a static const and a const as far as an array went.

    With the static const array declaration, the compiler knows
    that there is only one instance and can optimize away. With
    the const array declaration, the compiler was actually copying
    the const data to a temporary local variable that it created.

    Since these are integers, the above point may be moot. Your
    best bet is to look at the assembly language generated by
    the compiler and see what the difference really is.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Jun 16, 2004
    #6
  7. Alf P. Steinbach wrote:

    > * red floyd:
    >
    >>Gianni Mariani wrote:
    >>
    >>
    >>>Michael Klatt wrote:
    >>>
    >>>
    >>>>Is there any practical difference between a local variable in main()
    >>>>declared 'const' and one declared 'static const'?
    >>>>
    >>>>int main()
    >>>>{
    >>>> static const int i1(0);
    >>>> const int i2(0);
    >>>> return 0;
    >>>>}
    >>>
    >>>
    >>>Not with int but for non POD types, the difference is when the
    >>>destructor is called.

    >>
    >>Wouldn't it make a diff if main() was called recursively?

    >
    >
    > Not allowed.
    >

    why not?

    rlc
    Ronald Landheer-Cieslak, Jun 16, 2004
    #7
  8. * Ronald Landheer-Cieslak wrote:
    > Alf P. Steinbach wrote:
    >> * red floyd:
    >>>Wouldn't it make a diff if main() was called recursively?

    >>
    >> Not allowed.
    >>

    > why not?


    Because the standard sais so:

    From annex C, Clause 3.6:
    Main cannot be called recursively and cannot have its address taken
    Rationale: The main function may require special acions.
    --
    Robert Bauck Hamar
    Robert Bauck Hamar, Jun 16, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rakesh Sinha
    Replies:
    4
    Views:
    1,837
    Rakesh Sinha
    Jan 13, 2005
  2. Dave
    Replies:
    10
    Views:
    35,234
    Ron Natalie
    May 22, 2005
  3. Replies:
    11
    Views:
    1,084
  4. Javier
    Replies:
    2
    Views:
    540
    James Kanze
    Sep 4, 2007
  5. er
    Replies:
    3
    Views:
    373
Loading...

Share This Page