static const variables in main()

M

Michael Klatt

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;
}
 
G

Gianni Mariani

Michael said:
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.
 
R

red floyd

Gianni said:
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?
 
T

Thomas Matthews

Michael said:
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
 
R

Robert Bauck Hamar

* Ronald Landheer-Cieslak said:

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top