473,382 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

static const variables in main()

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;
}
Jul 22 '05 #1
7 2812
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.
Jul 22 '05 #2
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?
Jul 22 '05 #3
* 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?
Jul 22 '05 #4
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).
Jul 22 '05 #5
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

Jul 22 '05 #6
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
Jul 22 '05 #7
* 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
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: sks_cpp | last post by:
When do static members allocate memory? (at load time) How about static variables within a method? Where is the memory for static variables - is it allocated on the heap or does it have a...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
15
by: Bit byte | last post by:
I am writing a small parser object. I need to store keywords etc in lsts. Because this data is to be shared by all instances of my parser class, I have declared the variable as class variables...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
7
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
9
by: t | last post by:
Lippman's C++ Primer says that if you initialize a const static data member in a class, you still need to define the member outside the class but without an initializer. I tried it using Visual...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.