Invoking a function before execution of main()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manuel

    Invoking a function before execution of main()

    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
  • Christian W.

    #2
    Re: Invoking a function before execution of main()

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

    Well, IMHO there is no other way to realize that in C++.


    Comment

    • John Isaacks

      #3
      Re: Invoking a function before execution of main()

      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,e tc.

      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" <manueljoy@hotm ail.com> wrote in message
      news:b753d529.0 307180418.3c976 985@posting.goo gle.com...[color=blue]
      > 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[/color]



      Comment

      • Thomas Matthews

        #4
        Re: Invoking a function before execution of main()

        John Isaacks wrote:[color=blue]
        > 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,e tc.
        >
        > 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" <manueljoy@hotm ail.com> wrote in message
        > news:b753d529.0 307180418.3c976 985@posting.goo gle.com...
        >[color=green]
        >>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[/color][/color]

        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:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        • Andre Kostur

          #5
          Re: Invoking a function before execution of main()

          manueljoy@hotma il.com (Manuel) wrote in news:b753d529.0 307180418.3c976 985
          @posting.google .com:
          [color=blue]
          > 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.[/color]

          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.

          Comment

          • Andre Kostur

            #6
            Re: Invoking a function before execution of main()

            "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in
            news:vhg09pt2g2 9f93@corp.super news.com:
            [color=blue]
            > "Andre Kostur" <nntpspam@kostu r.net> wrote...[color=green]
            >> manueljoy@hotma il.com (Manuel) wrote in
            >> news:b753d529.0 307180418.3c976 985 @posting.google .com:
            >>[color=darkred]
            >> > 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.[/color]
            >>
            >> 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[/color]
            > global[color=green]
            >> 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.[/color]
            >
            > 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.[/color]

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

            Comment

            • John Isaacks

              #7
              Re: Invoking a function before execution of main()

              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.







              Comment

              Working...