By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

Is main() required?

P: n/a
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
Share this Question
Share on Google+
15 Replies


P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.