473,399 Members | 3,106 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,399 software developers and data experts.

Is main() required?

In C++ execution of a program starts already before execution of main().
The initialization of static variables defined outside the scope of main
is performed first. I could imagine a program where the whole job
can be done in the constructor and destructor of a static variable.
(Wouldn't that be the ultimate OOP program, just the creation and
deletion of an object?)
Does the C++ standard require to have a (dummy) main() function
in such a program?

F.Z.
Oct 13 '05 #1
15 2858
* Fred Zwarts:

Does the C++ standard require to have a (dummy) main() function
in [...] a program?


For a hosted implementation (that means one claiming to be full C++, one you
use to create ordinary applications for general computers), yes.

For a freestanding implementation (typical for embedded systems programming),
no.

--
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?
Oct 13 '05 #2
sat
Hi Freds,
yes its an interesting view you have. First, No one ever proclaimed that
C++ is a ultimate OOP language.
Second, this is almost what Java has brought into it while creating the
ultimate OOP (with its own headaches ofcourse).

No program can be executed without the entry point, so if global variables
were used as entry points, what happens when we have two objects getting
created under global scope?
who takes control of the program? Do the run parallely ? Is that what is
intended ?? or if serial .. why should one object come after other ?

hence main is necessary ( in c++ it is a global fucntion ) and in (java it
is a static member function of a class )

hence, we do not guarantee the execution method of global objects in C++,
but we guarantee the flow of the code by specifying an entry point, which is
absolutely necessary even in OOP..


"Fred Zwarts" <F.******@KVI.nl> wrote in message
news:di**********@info.service.rug.nl...
In C++ execution of a program starts already before execution of main().
The initialization of static variables defined outside the scope of main
is performed first. I could imagine a program where the whole job
can be done in the constructor and destructor of a static variable.
(Wouldn't that be the ultimate OOP program, just the creation and
deletion of an object?)
Does the C++ standard require to have a (dummy) main() function
in such a program?

F.Z.

Oct 13 '05 #3
Alf P. Steinbach wrote:
* Fred Zwarts:

Does the C++ standard require to have a (dummy) main() function
in [...] a program?


For a freestanding implementation (typical for embedded systems
programming), no.


Just to make it clear: The C++ standard doesn't require it in this case, but
the implementation may very well require it.

Oct 13 '05 #4
sat wrote:
if serial .. why should one object come after other ?


how about: "an order of appearance (from compiler's viewpoint)"?

Oct 13 '05 #5
In article <di**********@news.mch.sbs.de>, sat <sa**********@gmail.com> wrote:
"Fred Zwarts" <F.******@KVI.nl> wrote in message
news:di**********@info.service.rug.nl...
In C++ execution of a program starts already before execution of main().
The initialization of static variables defined outside the scope of main
is performed first. I could imagine a program where the whole job
can be done in the constructor and destructor of a static variable.
(Wouldn't that be the ultimate OOP program, just the creation and
deletion of an object?)
Does the C++ standard require to have a (dummy) main() function
in such a program?

yes its an interesting view you have. First, No one ever proclaimed that
C++ is a ultimate OOP language.
Second, this is almost what Java has brought into it while creating the
ultimate OOP (with its own headaches ofcourse).

No program can be executed without the entry point, so if global variables
were used as entry points, what happens when we have two objects getting
created under global scope?
who takes control of the program? Do the run parallely ? Is that what is
intended ?? or if serial .. why should one object come after other ?

hence main is necessary ( in c++ it is a global fucntion ) and in (java it
is a static member function of a class )

hence, we do not guarantee the execution method of global objects in C++,
but we guarantee the flow of the code by specifying an entry point, which is
absolutely necessary even in OOP..


Although it's true that a program, _OOP'd or not_, needs to begin
executing somewhere, it's not necessarily so that all programs
need a sole entry point, even though yes there are question of
initialization and other things which do need to be determined and
actually done somehow. "It just so happens" that in C++ one (main())
is defined as such, as was mentioned, in a so-called hosted environment
will be the one expected.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 13 '05 #6
In article <di*************@news.t-online.com>,
Rolf Magnus <ra******@t-online.de> wrote:
Alf P. Steinbach wrote:
* Fred Zwarts:

Does the C++ standard require to have a (dummy) main() function
in [...] a program?


For a freestanding implementation (typical for embedded systems
programming), no.


Just to make it clear: The C++ standard doesn't require it in this case, but
the implementation may very well require it.


Agreed. We've done many embedded ports of Comeau C++ where
the customer swore is couldn't be conforming, and we got it
to be so, including being hosted.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 13 '05 #7
"sat" <sa**********@gmail.com> wrote in message news:di**********@news.mch.sbs.de...
Hi Freds,
yes its an interesting view you have. First, No one ever proclaimed that
C++ is a ultimate OOP language.
Second, this is almost what Java has brought into it while creating the
ultimate OOP (with its own headaches ofcourse).

No program can be executed without the entry point, so if global variables
were used as entry points, what happens when we have two objects getting
created under global scope?
who takes control of the program? Do the run parallely ? Is that what is
intended ?? or if serial .. why should one object come after other ?
Since C++ already allows the initialization of static global variables
(and these variables are constructed before main() is called)
your questions may be asked already now even in the presence of a main program.
(Just search for "static initialization order fiasco".)
The presence or absence of a main program does not change these questions.

hence main is necessary ( in c++ it is a global fucntion ) and in (java it
is a static member function of a class )
I can already do now what I described. I only have to add a dummy main():

void main () {} // Yes, it should return int, it should have parameters.

The constructors of static global variables will do the job.
This is possible in the current C++ language.
I was only wondering whether (and why) such a dummy main() is required.
I don't see what such a dummy main() adds to the program, so why is it necessary?
(It is needed if interpretation of comand line parameters and/or the return of a value
is required, which is not the case for all programs.)
hence, we do not guarantee the execution method of global objects in C++,
but we guarantee the flow of the code by specifying an entry point, which is
absolutely necessary even in OOP..

I know that the order of the initialization of static global objects in different
compilation units is not defined. So, the code should not depend on this order.
Again, the presence of a main program does not solve this problem.


"Fred Zwarts" <F.******@KVI.nl> wrote in message
news:di**********@info.service.rug.nl...
In C++ execution of a program starts already before execution of main().
The initialization of static variables defined outside the scope of main
is performed first. I could imagine a program where the whole job
can be done in the constructor and destructor of a static variable.
(Wouldn't that be the ultimate OOP program, just the creation and
deletion of an object?)
Does the C++ standard require to have a (dummy) main() function
in such a program?

F.Z.


Oct 14 '05 #8
Tom
you are absolutely right, in mentioning that the questions which have been
put forth are appropriate even when main is used ( dummy main )
But, the importance of these questions become more prominent when we do not
have a main function at all.

Re-explaining.
consider the scenario when we do not have a main(), then we need to
compulosorily answer the questions put forth , in the previous posting.
Since, any program has a flow, which has to be decided before it is compiled
and executed, to have a business application.
But, consider the current scenario, where main() makes it mandatory
putting forth the compiler-rule where compiler says "look... I am not
responsible for the order in which your global data is going to get
initialized. But, I give you this convenience that, after all such
initializations, I am going to take up your main(), and from there on you
can control how I am going to interpret the flow of your program.".

The example program which you have described(dummy main()) can probably be
used construct smaller programs, but i dont see its real time use , for the
questions which were put forth in the previous posts of this discussion.
"Fred Zwarts" <F.******@KVI.nl> wrote in message
news:di**********@info.service.rug.nl...
"sat" <sa**********@gmail.com> wrote in message
news:di**********@news.mch.sbs.de...
Hi Freds,
yes its an interesting view you have. First, No one ever proclaimed
that
C++ is a ultimate OOP language.
Second, this is almost what Java has brought into it while creating the
ultimate OOP (with its own headaches ofcourse).

No program can be executed without the entry point, so if global variables
were used as entry points, what happens when we have two objects getting
created under global scope?
who takes control of the program? Do the run parallely ? Is that what is
intended ?? or if serial .. why should one object come after other ?
Since C++ already allows the initialization of static global variables
(and these variables are constructed before main() is called)
your questions may be asked already now even in the presence of a main
program.
(Just search for "static initialization order fiasco".)
The presence or absence of a main program does not change these questions.

hence main is necessary ( in c++ it is a global fucntion ) and in (java it
is a static member function of a class )
I can already do now what I described. I only have to add a dummy main():

void main () {} // Yes, it should return int, it should have
parameters.

The constructors of static global variables will do the job.
This is possible in the current C++ language.
I was only wondering whether (and why) such a dummy main() is required.
I don't see what such a dummy main() adds to the program, so why is it
necessary?
(It is needed if interpretation of comand line parameters and/or the return
of a value
is required, which is not the case for all programs.)
hence, we do not guarantee the execution method of global objects in C++,
but we guarantee the flow of the code by specifying an entry point, which
is
absolutely necessary even in OOP..

I know that the order of the initialization of static global objects in
different
compilation units is not defined. So, the code should not depend on this
order.
Again, the presence of a main program does not solve this problem.


"Fred Zwarts" <F.******@KVI.nl> wrote in message
news:di**********@info.service.rug.nl...
In C++ execution of a program starts already before execution of main().
The initialization of static variables defined outside the scope of main
is performed first. I could imagine a program where the whole job
can be done in the constructor and destructor of a static variable.
(Wouldn't that be the ultimate OOP program, just the creation and
deletion of an object?)
Does the C++ standard require to have a (dummy) main() function
in such a program?

F.Z.

Oct 14 '05 #9
Fred Zwarts wrote:
I can already do now what I described. I only have to add a dummy main():

void main () {} // Yes, it should return int, it should have
parameters.
No, in standard C++, it _must_ return int, and by just replacing 'void' with
'int', you would save a character of typing in addition to the lengthy
comment. And it doesn't need parameters if you don't need to evaluate
command line arguments.
I was only wondering whether (and why) such a dummy main() is required.
I don't see what such a dummy main() adds to the program, so why is it
necessary?


Because that's how C++ is defined. After construction of static objects,
main() gets called, and if that isn't there, you get an error. There is
nothing more to it.
hence, we do not guarantee the execution method of global objects in C++,
but we guarantee the flow of the code by specifying an entry point, which
is absolutely necessary even in OOP..


I know that the order of the initialization of static global objects in
different compilation units is not defined. So, the code should not depend
on this order. Again, the presence of a main program does not solve this
problem.


No, it doesn't, but it means that you still need one defined entry point. It
wouldn't be main(), but rather the constructor of your static object, but
that doesn't make much of a difference.

Oct 14 '05 #10

"Fred Zwarts" <F.******@KVI.nl> wrote in message
news:di**********@info.service.rug.nl...
"
In C++ execution of a program starts already before execution of main().
The initialization of static variables defined outside the scope of main
is performed first. I could imagine a program where the whole job
can be done in the constructor and destructor of a static variable.
(Wouldn't that be the ultimate OOP program, just the creation and
deletion of an object?)
"

No. All static variables have to be initalized, you can't refer to any other
static object from constructor of static object, except
those that are initialized in called functions.
In mt environment things are getting worse (you get undefined behavior)
if you access other static data from different thread then one which runs
initialization code.

"
Does the C++ standard require to have a (dummy) main() function
in such a program?
"

I have cases, when linker didn't initialized statics at all, but I have to
call "init_statics" "destroy_statics" manually, which wouldn't be possible
without main entry function.

Greetings, Bane.
Oct 14 '05 #11
Tom wrote:
Re-explaining...
compiler says "look... I am not
responsible for the order in which your global data is going to get
initialized. But, I give you this convenience that, after all such
initializations, I am going to take up your main(), and from there on you
can control how I am going to interpret the flow of your program."


so, in other words, one can think of "main" as "constructor" of
"hidden" global object, that is guaranteed to be initialized last among
all other globals?

Oct 14 '05 #12

ma************@gmail.com wrote:
Tom wrote:
Re-explaining...
compiler says "look... I am not
responsible for the order in which your global data is going to get
initialized. But, I give you this convenience that, after all such
initializations, I am going to take up your main(), and from there on you
can control how I am going to interpret the flow of your program."


so, in other words, one can think of "main" as "constructor" of
"hidden" global object, that is guaranteed to be initialized last among
all other globals?


No. This looks like this:
struct Program{
static Program(){ /* init statics */ }
static ~Program(){ /* destruct statics */ } // though c++ doesn't have
// static constructors and
destructors,
// it would be very good thing to
// have them as in D
static int main(){ /* work */ }
// ...
// static data
// ...
};

run time simply constructs static data, then calls main, then
destructs.
If module is run time linkable then each module have:
struct Module{
static Module(){/*...*/ }
static ~Module(){/* ... */ }
// module api
// module static data
};

That is how it is done.

Greetings, Bane.

Oct 14 '05 #13
In article <11**********************@o13g2000cwo.googlegroups .com>,
<ma************@gmail.com> wrote:
Tom wrote:
Re-explaining...
compiler says "look... I am not
responsible for the order in which your global data is going to get
initialized. But, I give you this convenience that, after all such
initializations, I am going to take up your main(), and from there on you
can control how I am going to interpret the flow of your program."


so, in other words, one can think of "main" as "constructor" of
"hidden" global object, that is guaranteed to be initialized last among
all other globals?


As I understand the above discussion, there are no such general
claims in Standard C++.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 14 '05 #14
Greg Comeau wrote:
so, in other words, one can think of "main" as "constructor" of
"hidden" global object, that is guaranteed to be initialized last among
all other globals?


As I understand the above discussion, there are no such general
claims in Standard C++.


I said, "one can think of "main" as....", and not "c++ standard sais
"main" is...."

There's a difference.

Oct 16 '05 #15
In article <11**********************@g14g2000cwa.googlegroups .com>,
<ma************@gmail.com> wrote:
Greg Comeau wrote:
>so, in other words, one can think of "main" as "constructor" of
>"hidden" global object, that is guaranteed to be initialized last among
>all other globals?


As I understand the above discussion, there are no such general
claims in Standard C++.


I said, "one can think of "main" as....", and not "c++ standard sais
"main" is...."

There's a difference.


I don't and didn't disagree there is a difference.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 16 '05 #16

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

Similar topics

19
by: Steven T. Hatton | last post by:
The short sample program listed below has some features that I find to be bad style. In particular, they fail to communicate the connection between names used in this program and the location in...
9
by: Neelesh | last post by:
Hi all, it is strange that the following code compiles and runs (tested with g++ 3.4.2) #include <iostream> using namespace std; int main(int a, int b, int c, int d, int e) { cout <<...
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
1
by: BillZondlo | last post by:
Can someone look at this and tell me why, when I call this from my main app, it displays fine (fades form in and out) but then before my main app displays, I see other dialog boxes flash...
7
by: | last post by:
Hello, Please bear with me as this is a very basic question. Using Sub Main() within a console application gets executed when the console application first starts. Does the same hold true for...
4
by: Bob Day | last post by:
Using VS 2003, VB.Net, MSDE... The example below from help shows that the Sub Main contains an explicit Application.Run. Is this required? Are there pros and cons of doing it this way? I have...
7
by: Terry | last post by:
I have a Mainform with a Statusbar. When opening another form or doing some processing I want to display info in the Statusbar of the Mainform. I have read a lot of articles on this & have come up...
1
by: gleave | last post by:
Hi, I have a problem with subforms. I want to lock a subform until all the required data is inputted in the main form. The required fields in main form: Invoice Number Invoice to The main...
1
by: HarishAdea | last post by:
Hi, I am trying to run the JAVA pgm, but it is giving error as "selection does not contain a main type". The filename is "ScoreLeadSummary.java" when i try to run it or debug,it gives the pop...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.