Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Is main() a callback function?

Is main() a callback function?

Scheduled Pinned Locked Moved The Lounge
question
44 Posts 15 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rick York

    The non-re-entrancy part comes about because you can't call it yourself. It's not a callback in the strict sense of the term but in effect it is if you think of it as the designated function for the OS to call to run the program. It is not specified in code (this is why it fails the strict definition) but it is implicitly known to the linker and can be overridden. In the case of programs for Windows, it IS overridden to be WinMain.

    "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #21

    Rick York wrote:

    you can't call it yourself.

    Indeed you can, but you need to know what you are doing.

    Rick York wrote:

    but it is implicitly known to the linker

    Not quite, there is a reference to it in the run time libraries which must be satisfied at link time.

    Rick York wrote:

    in the case of programs for Windows, it IS overridden to be WinMain.

    But there is a main() inside the Windows libraries, which again gets called by the run time (unless it has changed in the last 20+ years). And that then calls in to WinMain.

    R 1 Reply Last reply
    0
    • R Rob Philpott

      Interesting, I didn't know/have forgotten about the re-entrancy. Seems you can do it in C#, but that's with a capital 'M'ain, and is a language for wimps.

      Regards, Rob Philpott.

      H Offline
      H Offline
      honey the codewitch
      wrote on last edited by
      #22

      C is for wimps Real programmers use butterflies

      Real programmers use butterflies

      T 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        It called me back once, but it was drunk at the time.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

        D Offline
        D Offline
        DRHuff
        wrote on last edited by
        #23

        Did it say - "I want to C you again!" ? And did you reply #ly that you had moved on? :-D

        I, for one, like Roman Numerals.

        OriginalGriffO 1 Reply Last reply
        0
        • R Rob Philpott

          I can't make up my mind.

          Regards, Rob Philpott.

          H Offline
          H Offline
          honey the codewitch
          wrote on last edited by
          #24

          I figure every function is a callback function - it's just a matter of perspective. :-D

          Real programmers use butterflies

          1 Reply Last reply
          0
          • R Rob Philpott

            I can't make up my mind.

            Regards, Rob Philpott.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #25

            It's an "entry point"; "Main" wasn't something that was part of a "response", other than "call static Main in class x".

            It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

            1 Reply Last reply
            0
            • R Rob Philpott

              I can't make up my mind.

              Regards, Rob Philpott.

              G Offline
              G Offline
              Gary Wheeler
              wrote on last edited by
              #26

              If you take the view that your application is an interruption to a busy operating system's schedule, but it figures it should let you do your thing before you whine and get all bitchy about it, then yes, it is a callback function.

              Software Zen: delete this;

              1 Reply Last reply
              0
              • R Rob Philpott

                I can't make up my mind.

                Regards, Rob Philpott.

                M Offline
                M Offline
                Mark_Wallace
                wrote on last edited by
                #27

                Are you asking if it's masculine or feminine? I've no idea, and I don't want to look, just in case.

                I wanna be a eunuchs developer! Pass me a bread knife!

                1 Reply Last reply
                0
                • R raddevus

                  I was just reading this yesterday[^]

                  Quote:

                  All C++ programs must have a main function. If you try to compile a C++ .exe project without a main function, the compiler will raise an error. (Dynamic-link libraries and static libraries don't have a main function.) The main function is where your source code begins execution, but before a program enters the main function, all static class members without explicit initializers are set to zero. In Microsoft C++, global static objects are also initialized before entry to main. Several restrictions apply to the main function that do not apply to any other C++ functions. The main function: * Cannot be overloaded (see Function Overloading). * Cannot be declared as inline. * Cannot be declared as static. * Cannot have its address taken. * Cannot be called.

                  But, maybe you are thinking it is a callback from the OS? Or maybe you're just asking a rhetorical question? :)

                  K Offline
                  K Offline
                  k5054
                  wrote on last edited by
                  #28

                  raddevus wrote:

                  * Cannot have its address taken. * Cannot be called.

                  Lies. You can call main, and you can take its address. This compiles and runs in VS2019:

                  #include int main(int argc, char **argv)
                  {
                  if(argc <= 1) {
                  auto main_ptr{main};
                  std::cout << "pointer to main = " << main_ptr << std::endl;
                  std::cout << "exiting ..." << std::endl;
                  return 0;
                  }
                  std::cout << "argc = " << argc << std::endl;
                  main(--argc, argv);
                  }

                  Interestingly, in linux you auto main_ptr{main} is 1, but for windows it looks like an address: 0008151E Update: I should also point out that the instances where you might need to call main from within you program are vanishingly small. In general, if you think you need to, you're almost certainly wrong.

                  R P 2 Replies Last reply
                  0
                  • D DRHuff

                    Did it say - "I want to C you again!" ? And did you reply #ly that you had moved on? :-D

                    I, for one, like Roman Numerals.

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #29

                    Difficult to tell, it was yelling a lot and slurring it's words.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    D 1 Reply Last reply
                    0
                    • L Lost User

                      Rick York wrote:

                      you can't call it yourself.

                      Indeed you can, but you need to know what you are doing.

                      Rick York wrote:

                      but it is implicitly known to the linker

                      Not quite, there is a reference to it in the run time libraries which must be satisfied at link time.

                      Rick York wrote:

                      in the case of programs for Windows, it IS overridden to be WinMain.

                      But there is a main() inside the Windows libraries, which again gets called by the run time (unless it has changed in the last 20+ years). And that then calls in to WinMain.

                      R Offline
                      R Offline
                      Rick York
                      wrote on last edited by
                      #30

                      Actually things are a little different. In the Visual Studio source, VC/Tools/MSVC/**version**/crt/src/vcruntime/vcstartup_internal.h has the prototypes and the calls are in exe_common.inl. WinMain and main are two different calls along with their wide character versions.

                      "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                      L 1 Reply Last reply
                      0
                      • K k5054

                        raddevus wrote:

                        * Cannot have its address taken. * Cannot be called.

                        Lies. You can call main, and you can take its address. This compiles and runs in VS2019:

                        #include int main(int argc, char **argv)
                        {
                        if(argc <= 1) {
                        auto main_ptr{main};
                        std::cout << "pointer to main = " << main_ptr << std::endl;
                        std::cout << "exiting ..." << std::endl;
                        return 0;
                        }
                        std::cout << "argc = " << argc << std::endl;
                        main(--argc, argv);
                        }

                        Interestingly, in linux you auto main_ptr{main} is 1, but for windows it looks like an address: 0008151E Update: I should also point out that the instances where you might need to call main from within you program are vanishingly small. In general, if you think you need to, you're almost certainly wrong.

                        R Offline
                        R Offline
                        Rick York
                        wrote on last edited by
                        #31

                        That has always been my thought.

                        "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                        1 Reply Last reply
                        0
                        • H honey the codewitch

                          C is for wimps Real programmers use butterflies

                          Real programmers use butterflies

                          T Offline
                          T Offline
                          TheGreatAndPowerfulOz
                          wrote on last edited by
                          #32

                          Real programmers use galaxy-sized gravitic lenses.

                          #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                          H 1 Reply Last reply
                          0
                          • K k5054

                            raddevus wrote:

                            * Cannot have its address taken. * Cannot be called.

                            Lies. You can call main, and you can take its address. This compiles and runs in VS2019:

                            #include int main(int argc, char **argv)
                            {
                            if(argc <= 1) {
                            auto main_ptr{main};
                            std::cout << "pointer to main = " << main_ptr << std::endl;
                            std::cout << "exiting ..." << std::endl;
                            return 0;
                            }
                            std::cout << "argc = " << argc << std::endl;
                            main(--argc, argv);
                            }

                            Interestingly, in linux you auto main_ptr{main} is 1, but for windows it looks like an address: 0008151E Update: I should also point out that the instances where you might need to call main from within you program are vanishingly small. In general, if you think you need to, you're almost certainly wrong.

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #33

                            Hold my Jolt Cola...

                            1 Reply Last reply
                            0
                            • T TheGreatAndPowerfulOz

                              Real programmers use galaxy-sized gravitic lenses.

                              #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                              H Offline
                              H Offline
                              honey the codewitch
                              wrote on last edited by
                              #34

                              Focused using butterflies

                              Real programmers use butterflies

                              T 1 Reply Last reply
                              0
                              • D DRHuff

                                Well it never called me back - and I thought we had such a connection... :(( :(( :mad: :(( :((

                                I, for one, like Roman Numerals.

                                T Offline
                                T Offline
                                TheGreatAndPowerfulOz
                                wrote on last edited by
                                #35

                                Did you give it your number, baby? ;P

                                #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                                1 Reply Last reply
                                0
                                • H honey the codewitch

                                  Focused using butterflies

                                  Real programmers use butterflies

                                  T Offline
                                  T Offline
                                  TheGreatAndPowerfulOz
                                  wrote on last edited by
                                  #36

                                  honey the codewitch wrote:

                                  Focused using butterflies

                                  that are manipulated using gravitic lenses

                                  #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                                  H 1 Reply Last reply
                                  0
                                  • T TheGreatAndPowerfulOz

                                    honey the codewitch wrote:

                                    Focused using butterflies

                                    that are manipulated using gravitic lenses

                                    #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                                    H Offline
                                    H Offline
                                    honey the codewitch
                                    wrote on last edited by
                                    #37

                                    created from the silk of butterfly cocoons :-D

                                    Real programmers use butterflies

                                    T 1 Reply Last reply
                                    0
                                    • OriginalGriffO OriginalGriff

                                      Difficult to tell, it was yelling a lot and slurring it's words.

                                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                      D Offline
                                      D Offline
                                      DRHuff
                                      wrote on last edited by
                                      #38

                                      So it was yaccing with a lisp?

                                      I, for one, like Roman Numerals.

                                      1 Reply Last reply
                                      0
                                      • R Rob Philpott

                                        That reminds me of a thing I read some time back, titled something along the lines of 'the 50 things Windows does before hitting main()'. Can't find it but it's out there somewhere, by one of the SysInternals lot I think. It was both interesting and really boring at the same time.

                                        Regards, Rob Philpott.

                                        R Offline
                                        R Offline
                                        Rick York
                                        wrote on last edited by
                                        #39

                                        If you have Visual Studio you can see what the C run-time library does by looking at its source. I was checking that out just today.

                                        "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                                        1 Reply Last reply
                                        0
                                        • R Rick York

                                          Actually things are a little different. In the Visual Studio source, VC/Tools/MSVC/**version**/crt/src/vcruntime/vcstartup_internal.h has the prototypes and the calls are in exe_common.inl. WinMain and main are two different calls along with their wide character versions.

                                          "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                                          L Offline
                                          L Offline
                                          Lost User
                                          wrote on last edited by
                                          #40

                                          I suspected this might be the case, but I have not had access to the source for a number of years.

                                          R 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups