Invoking a function before execution of main()

Discussion in 'C++' started by Manuel, Jul 18, 2003.

  1. Manuel

    Manuel Guest

    Can I invoke a function before main
    I could do it by invoking it in a Global object's constructor .
    Is there any other method other than this.

    Manuel
     
    Manuel, Jul 18, 2003
    #1
    1. Advertising

  2. Manuel

    Christian W. Guest

    > Can I invoke a function before main
    > I could do it by invoking it in a Global object's constructor .
    > Is there any other method other than this.


    Well, IMHO there is no other way to realize that in C++.
     
    Christian W., Jul 18, 2003
    #2
    1. Advertising

  3. Manuel

    John Isaacks Guest

    If you think on it for a little while you should be able to eliminate the
    need with a simple redesign.

    What's the difference between
    foo() begin called before main() and

    main()
    {
    foo();
    // rest of code here
    }

    global objects are in general not a good idea,
    keep them to simple data types like int,long,char,etc.

    The debug code on some machines (Microsoft Visual C++)
    checks for memory leaks prior to destructing global objects
    so objects like CString's will look like memory leaks
    when they aren't.




    "Manuel" <> wrote in message
    news:...
    > Can I invoke a function before main
    > I could do it by invoking it in a Global object's constructor .
    > Is there any other method other than this.
    >
    > Manuel
     
    John Isaacks, Jul 18, 2003
    #3
  4. John Isaacks wrote:
    > If you think on it for a little while you should be able to eliminate the
    > need with a simple redesign.
    >
    > What's the difference between
    > foo() begin called before main() and
    >
    > main()
    > {
    > foo();
    > // rest of code here
    > }
    >
    > global objects are in general not a good idea,
    > keep them to simple data types like int,long,char,etc.
    >
    > The debug code on some machines (Microsoft Visual C++)
    > checks for memory leaks prior to destructing global objects
    > so objects like CString's will look like memory leaks
    > when they aren't.
    >
    >
    >
    >
    > "Manuel" <> wrote in message
    > news:...
    >
    >>Can I invoke a function before main
    >> I could do it by invoking it in a Global object's constructor .
    >> Is there any other method other than this.
    >>
    >> Manuel


    1. Don't top-post, replies are either interspersed or
    appended at the bottom.

    There is much difference between a function being called
    before main() and after. There is a lot that goes on
    before the main() function is executed. For example,
    constructors of global objects get called before the
    main() function; but their order is unspecified.

    The common method to have code executed before the
    main() function is to consult your compiler or linker
    documentation and see what function(s) get called
    before main(). Many compilers for embedded systems
    have "hooks" for functions to be called before
    main(). If you have a function you want called
    before main(), it may have to be written in assembly
    depending on when the C and C++ initialization
    takes place.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jul 18, 2003
    #4
  5. Manuel

    Andre Kostur Guest

    (Manuel) wrote in news:b753d529.0307180418.3c976985
    @posting.google.com:

    > Can I invoke a function before main
    > I could do it by invoking it in a Global object's constructor .
    > Is there any other method other than this.


    Individual implementations may have extensions to do it. However, from a
    strictly C++ standpoint, all you can do is rely on a global object's
    constructor. However one thing to keep in mine is what the order of global
    object constructors is.... within a single translation unit (generally
    speaking, 1 .cpp file plus everything it includes) global objects are
    constructed in the order that they are declared. The twist is that
    _between_ translation units, it is undefined as to what order the
    translation units are constructed in. So if you have two translation
    units, with two global objects each (say A1, A2, B1, and B2), the order of
    construction could be either A1 - A2 - B1 - B2, or B1 - B2 - A1 - A2. It's
    up to the compiler to make that decision.
     
    Andre Kostur, Jul 18, 2003
    #5
  6. Manuel

    Andre Kostur Guest

    "Victor Bazarov" <> wrote in
    news::

    > "Andre Kostur" <> wrote...
    >> (Manuel) wrote in
    >> news:b753d529.0307180418.3c976985 @posting.google.com:
    >>
    >> > Can I invoke a function before main
    >> > I could do it by invoking it in a Global object's constructor .
    >> > Is there any other method other than this.

    >>
    >> Individual implementations may have extensions to do it. However,
    >> from a strictly C++ standpoint, all you can do is rely on a global
    >> object's constructor. However one thing to keep in mine is what the
    >> order of

    > global
    >> object constructors is.... within a single translation unit
    >> (generally speaking, 1 .cpp file plus everything it includes) global
    >> objects are constructed in the order that they are declared. The
    >> twist is that _between_ translation units, it is undefined as to what
    >> order the translation units are constructed in. So if you have two
    >> translation units, with two global objects each (say A1, A2, B1, and
    >> B2), the order of construction could be either A1 - A2 - B1 - B2, or
    >> B1 - B2 - A1 - A2.

    >
    > You say it like all objects are constructed in one unit and
    > then all objects are constructed in the other unit. I cannot
    > find anything to substantiate that, can you? Therefore, it
    > seems that the orders A1-B1-B2-A2, B1-A1-B2-A2, etc., are just
    > as possible.


    True... I suppose theoretically their constructors could be interleaved.
    About all you can rely on is that A1 will be constructed before A2 (and
    likewise for B1 and B2).
     
    Andre Kostur, Jul 18, 2003
    #6
  7. Manuel

    John Isaacks Guest

    All things have a beginning and that is the reason for main().

    It's not good practice to have items as global because you can't
    control the order in which the items are constructed.

    If you remove your global items and have them created ( with the "new" )
    from instead of main()
    you can control the order of things.
     
    John Isaacks, Jul 19, 2003
    #7
    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. Alex Vinokur
    Replies:
    1
    Views:
    421
    Jakob Bieling
    Jul 24, 2003
  2. Chris Torek

    Re: Invoking fuction before main

    Chris Torek, Jul 18, 2003, in forum: C Programming
    Replies:
    5
    Views:
    363
    Dave Thompson
    Jul 23, 2003
  3. Dan Pop

    Re: Invoking fuction before main

    Dan Pop, Jul 18, 2003, in forum: C Programming
    Replies:
    8
    Views:
    428
    Dan Pop
    Jul 21, 2003
  4. Joona I Palaste

    Re: Invoking fuction before main

    Joona I Palaste, Jul 19, 2003, in forum: C Programming
    Replies:
    5
    Views:
    359
    Chris Dollin
    Jul 21, 2003
  5. ravi
    Replies:
    28
    Views:
    1,023
    Richard Heathfield
    Sep 26, 2007
Loading...

Share This Page