468,505 Members | 1,938 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

Start point of execution of a program

Hello,
I know that the main( ) function is the starting point of execution in a C++
program.
What if an object is in the global space before main()?
Where is the constructor of the class object called? Before main() or after
main()?

Subrat@Bangalore
Aug 23 '06 #1
16 3820
subrat wrote:
Hello,
I know that the main( ) function is the starting point of execution in a C++
program.
What if an object is in the global space before main()?
Where is the constructor of the class object called? Before main() or after
main()?
Before, it were called after main, it wouldn't be much use as the
application would have terminated!

--
Ian Collins.
Aug 23 '06 #2
Before main().
subrat wrote:
Hello,
I know that the main( ) function is the starting point of execution in a C++
program.
What if an object is in the global space before main()?
Where is the constructor of the class object called? Before main() or after
main()?

Subrat@Bangalore
Aug 23 '06 #3
Can u give an example?
I am not able to figure it out?
<Is********@gmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
Before main().
subrat wrote:
Hello,
I know that the main( ) function is the starting point of execution in a
C++
program.
What if an object is in the global space before main()?
Where is the constructor of the class object called? Before main() or
after
main()?

Subrat@Bangalore

Aug 23 '06 #4

subrat wrote:
Hello,
I know that the main( ) function is the starting point of execution in a C++
program.
What if an object is in the global space before main()?
Where is the constructor of the class object called? Before main() or after
main()?

Subrat@Bangalore
It is implementation-defined. The very first (hidden) statement in main
might very well initialise global objects. This is one reason that you
must not call main yourself.

Kind regards
Peter

Aug 23 '06 #5
peter koch wrote:
It is implementation-defined. The very first (hidden) statement in main
might very well initialise global objects.
No, it can't work like that (think exceptions from globals). However,
there
may be a hidden function which does roughly the same: First initialise
globals,
then call main().

HTH,
Michiel Salters

Aug 23 '06 #6

Mi*************@tomtom.com wrote:
peter koch wrote:
It is implementation-defined. The very first (hidden) statement in main
might very well initialise global objects.

No, it can't work like that (think exceptions from globals). However,
there
may be a hidden function which does roughly the same: First initialise
globals,
then call main().

HTH,
Michiel Salters
I can't see the problem with exceptions from globals - could you
elaborate?

I believe that main on cfront has the behaviour I describe - but you
could think in function-try blocks?

/Peter

Aug 23 '06 #7
Mi*************@tomtom.com wrote:
peter koch wrote:
>It is implementation-defined. The very first (hidden) statement in main
might very well initialise global objects.

No, it can't work like that (think exceptions from globals). However,
there
may be a hidden function which does roughly the same: First initialise
globals,
then call main().
It sure as hell can work that way, and I've seen compilers that do it.
The calling sequence to main jumps to an internal function (_main) that
does all the dynamic global intialization.

I've also seen other compilers that start at an init function that does
all the global init and then jumps to main().
Aug 23 '06 #8
In article <ec**********@news4.fe.internet.bosch.com>,
subrat <su***********@in.bosch.comwrote:
>I know that the main( ) function is the starting point of execution in a C++
program.
What if an object is in the global space before main()?
That's allowed. main() being the so-called start of the program
is kind of wishy-washy.
>Where is the constructor of the class object called? Before main() or after
main()?
Assuming you mean "during the executoin of main()" then either.
It is implementation defined, although there is a whole set
of rules dictating C++ initialization.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
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?
Aug 23 '06 #9
In article <4l************@individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>subrat wrote:
>Hello,
I know that the main( ) function is the starting point of execution in a C++
program.
What if an object is in the global space before main()?
Where is the constructor of the class object called? Before main() or after
main()?
Before, it were called after main, it wouldn't be much use as the
application would have terminated!
Think OP meant before or after the execution of the first statement
of main. It need not be before. But there are requirements to that,
like it can be post-poned until the first use of somethinging the
translation unit in question, but even at that it is more involved
because there is different orderings possible as well there can
even be different levels of initialization possible.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
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?
Aug 23 '06 #10
In article <ec**********@news4.fe.internet.bosch.com>,
subrat <su***********@in.bosch.comwrote:
><Is********@gmail.comwrote in message
news:11*********************@i3g2000cwc.googlegro ups.com...
>Before main().
subrat wrote:
Hello,
I know that the main( ) function is the starting point of execution in a
C++
program.
What if an object is in the global space before main()?
Where is the constructor of the class object called? Before main() or
after
main()?
Can u give an example?
I am not able to figure it out?
The question is, given say this:

struct xyz { ... };

xyz X;

int main()
{
// Is X already intiialized here or not
}

The answer is that it is implementation defined with some
strings attached, whether it is, or whether it is delayed until
"fist use", etc.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
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?
Aug 23 '06 #11
In article <11*********************@h48g2000cwc.googlegroups. com>,
<Mi*************@tomtom.comwrote:
>peter koch wrote:
>It is implementation-defined. The very first (hidden) statement in main
might very well initialise global objects.

No, it can't work like that (think exceptions from globals). However,
there may be a hidden function which does roughly the same:
First initialise globals, then call main().
I must be misunderstanding, why do you think it can't be done that way?
Either they are caught, or terminate() is called, no?
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
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?
Aug 23 '06 #12
In article <11**********************@h48g2000cwc.googlegroups .com>,
peter koch <pe***************@gmail.comwrote:
>
Mi*************@tomtom.com wrote:
>peter koch wrote:
It is implementation-defined. The very first (hidden) statement in main
might very well initialise global objects.

No, it can't work like that (think exceptions from globals). However,
there
may be a hidden function which does roughly the same: First initialise
globals,
then call main().

HTH,
Michiel Salters

I can't see the problem with exceptions from globals - could you
elaborate?

I believe that main on cfront has the behaviour I describe - but you
could think in function-try blocks?
Indeed cfront did that. Many versions of Comeau C++ did and still do too
(some don't, there are many schemes possible).
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
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?
Aug 23 '06 #13
subrat posted:
Hello,
I know that the main( ) function is the starting point of execution in a
C++ program.
What if an object is in the global space before main()?
Where is the constructor of the class object called? Before main() or
after main()?

The following program prints "Hello World".

#include <iostream>
using std::cout;

class MyClass {
public:

MyClass()
{
cout << "Hello ";
}
};

MyClass global_object;

int main()
{
cout << "World\n";
}

--

Frederick Gotham
Aug 23 '06 #14
Is********@gmail.com wrote:
Before main().
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Brian (spreading the word)

Aug 23 '06 #15
subrat wrote:
Can u give an example?
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

And don't use abreviations like "u".

Brian (netiquette makes good netizens)
Aug 23 '06 #16
Default User wrote:
subrat wrote:
>Can u give an example?

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

And don't use abreviations like "u".

Brian (netiquette makes good netizens)
And think out your replies before you send them.
Aug 23 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.