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. | | | | re: Is main() required?
* Fred Zwarts:[color=blue]
>
> Does the C++ standard require to have a (dummy) main() function
> in [...] a program?[/color]
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? | | | | re: Is main() required?
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.Zwarts@KVI.nl> wrote in message
news:dil1gh$l0m$1@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. | | | | re: Is main() required?
Alf P. Steinbach wrote:
[color=blue]
> * Fred Zwarts:[color=green]
>>
>> Does the C++ standard require to have a (dummy) main() function
>> in [...] a program?[/color]
>
> For a freestanding implementation (typical for embedded systems
> programming), no.[/color]
Just to make it clear: The C++ standard doesn't require it in this case, but
the implementation may very well require it. | | | | re: Is main() required?
sat wrote:[color=blue]
> if serial .. why should one object come after other ?[/color]
how about: "an order of appearance (from compiler's viewpoint)"? | | | | re: Is main() required?
In article <dil8ji$qsp$1@news.mch.sbs.de>, sat <satish.lalam@gmail.com> wrote:[color=blue]
>"Fred Zwarts" <F.Zwarts@KVI.nl> wrote in message
>news:dil1gh$l0m$1@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..[/color]
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? | | | | re: Is main() required?
In article <dilc0o$mq6$00$2@news.t-online.com>,
Rolf Magnus <ramagnus@t-online.de> wrote:[color=blue]
>Alf P. Steinbach wrote:
>[color=green]
>> * Fred Zwarts:[color=darkred]
>>>
>>> Does the C++ standard require to have a (dummy) main() function
>>> in [...] a program?[/color]
>>
>> For a freestanding implementation (typical for embedded systems
>> programming), no.[/color]
>
>Just to make it clear: The C++ standard doesn't require it in this case, but
>the implementation may very well require it.[/color]
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? | | | | re: Is main() required?
"sat" <satish.lalam@gmail.com> wrote in message news:dil8ji$qsp$1@news.mch.sbs.de...[color=blue]
> 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 ?[/color]
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.
[color=blue]
>
> hence main is necessary ( in c++ it is a global fucntion ) and in (java it
> is a static member function of a class )[/color]
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.)
[color=blue]
> 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..
> [/color]
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.
[color=blue]
>
>
> "Fred Zwarts" <F.Zwarts@KVI.nl> wrote in message
> news:dil1gh$l0m$1@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.
>
>
>[/color] | | | | re: Is main() required?
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.Zwarts@KVI.nl> wrote in message
news:dinm9s$ar9$1@info.service.rug.nl...
"sat" <satish.lalam@gmail.com> wrote in message
news:dil8ji$qsp$1@news.mch.sbs.de...[color=blue]
> 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 ?[/color]
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.
[color=blue]
>
> hence main is necessary ( in c++ it is a global fucntion ) and in (java it
> is a static member function of a class )[/color]
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.)
[color=blue]
> 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..
>[/color]
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.
[color=blue]
>
>
> "Fred Zwarts" <F.Zwarts@KVI.nl> wrote in message
> news:dil1gh$l0m$1@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.
>
>
>[/color] | | | | re: Is main() required?
Fred Zwarts wrote:
[color=blue]
> 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.[/color]
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.
[color=blue]
> 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?[/color]
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.
[color=blue][color=green]
>> 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..[/color]
>
> 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.[/color]
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. | | | | re: Is main() required?
"Fred Zwarts" <F.Zwarts@KVI.nl> wrote in message
news:dil1gh$l0m$1@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. | | | | re: Is main() required?
Tom wrote:[color=blue]
> 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."[/color]
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? | | | | re: Is main() required? makc.the.great@gmail.com wrote:[color=blue]
> Tom wrote:[color=green]
> > 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."[/color]
>
> 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?[/color]
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. | | | | re: Is main() required?
In article <1129300758.589396.315340@o13g2000cwo.googlegroups .com>,
<makc.the.great@gmail.com> wrote:[color=blue]
>Tom wrote:[color=green]
>> 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."[/color]
>
>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?[/color]
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? | | | | re: Is main() required?
Greg Comeau wrote:[color=blue][color=green]
> >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?[/color]
>
> As I understand the above discussion, there are no such general
> claims in Standard C++.[/color]
I said, "one can think of "main" as....", and not "c++ standard sais
"main" is...."
There's a difference. | | | | re: Is main() required?
In article <1129453393.240126.298020@g14g2000cwa.googlegroups .com>,
<makc.the.great@gmail.com> wrote:[color=blue]
>Greg Comeau wrote:[color=green][color=darkred]
>> >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?[/color]
>>
>> As I understand the above discussion, there are no such general
>> claims in Standard C++.[/color]
>
>I said, "one can think of "main" as....", and not "c++ standard sais
>"main" is...."
>
>There's a difference.[/color]
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? |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|