Start point of execution of a program

S

subrat

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
 
I

Ian Collins

subrat said:
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!
 
P

peter koch

subrat said:
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
 
M

Michiel.Salters

peter said:
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
 
P

peter koch

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
 
R

Ron Natalie

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().
 
G

Greg Comeau

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.
 
G

Greg Comeau

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.
 
G

Greg Comeau

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.
 
G

Greg Comeau

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?
 
G

Greg Comeau

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).
 
F

Frederick Gotham

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";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top