Start point of execution of a program

Discussion in 'C++' started by subrat, Aug 23, 2006.

  1. subrat

    subrat Guest

    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
     
    subrat, Aug 23, 2006
    #1
    1. Advertising

  2. subrat

    Ian Collins Guest

    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.
     
    Ian Collins, Aug 23, 2006
    #2
    1. Advertising

  3. subrat

    Guest

    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, 2006
    #3
  4. subrat

    subrat Guest

    Can u give an example?
    I am not able to figure it out?
    <> wrote in message
    news:...
    > 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

    >
     
    subrat, Aug 23, 2006
    #4
  5. subrat

    peter koch Guest

    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
     
    peter koch, Aug 23, 2006
    #5
  6. subrat

    Guest

    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, 2006
    #6
  7. subrat

    peter koch Guest

    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
     
    peter koch, Aug 23, 2006
    #7
  8. subrat

    Ron Natalie Guest

    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().
     
    Ron Natalie, Aug 23, 2006
    #8
  9. subrat

    Greg Comeau Guest

    In article <ech4p3$kej$>,
    subrat <> wrote:
    >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?
     
    Greg Comeau, Aug 23, 2006
    #9
  10. subrat

    Greg Comeau Guest

    In article <>,
    Ian Collins <> wrote:
    >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?
     
    Greg Comeau, Aug 23, 2006
    #10
  11. subrat

    Greg Comeau Guest

    In article <ech8hm$ona$>,
    subrat <> wrote:
    ><> wrote in message
    >news:...
    >> 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?
     
    Greg Comeau, Aug 23, 2006
    #11
  12. subrat

    Greg Comeau Guest

    In article <>,
    <> 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().


    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?
     
    Greg Comeau, Aug 23, 2006
    #12
  13. subrat

    Greg Comeau Guest

    In article <>,
    peter koch <> wrote:
    >
    > 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?
     
    Greg Comeau, Aug 23, 2006
    #13
  14. 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
     
    Frederick Gotham, Aug 23, 2006
    #14
  15. subrat

    Default User Guest

    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)
     
    Default User, Aug 23, 2006
    #15
  16. subrat

    Default User Guest

    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)
     
    Default User, Aug 23, 2006
    #16
  17. subrat

    W Marsh Guest

    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.
     
    W Marsh, Aug 23, 2006
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gopi

    point to point protocol

    Gopi, Jul 13, 2004, in forum: VHDL
    Replies:
    1
    Views:
    477
    Mike Treseler
    Jul 13, 2004
  2. H aka N
    Replies:
    15
    Views:
    15,671
    Ben Jones
    Mar 2, 2006
  3. krishnakant Mane
    Replies:
    3
    Views:
    254
    Joshua J. Kugler
    Jan 20, 2007
  4. KevinSimonson

    Start of Execution in a C++ Program

    KevinSimonson, Oct 19, 2010, in forum: C++
    Replies:
    7
    Views:
    1,857
    Geoff
    Oct 20, 2010
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,349
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page