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 Offline
    R Offline
    Rob Philpott
    wrote on last edited by
    #1

    I can't make up my mind.

    Regards, Rob Philpott.

    R R L OriginalGriffO D 13 Replies Last reply
    0
    • R Rob Philpott

      I can't make up my mind.

      Regards, Rob Philpott.

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

      Yes, it is, and it's not re-entrant.

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

      R 1 Reply Last reply
      0
      • R Rob Philpott

        I can't make up my mind.

        Regards, Rob Philpott.

        R Offline
        R Offline
        raddevus
        wrote on last edited by
        #3

        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? :)

        R K 2 Replies 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? :)

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          Just pondering it really. Yes, I meant in effect it was called from the OS. Must admit I didn't know you couldn't call main() yourself - obviously never tried. Or quite possibly forgotten. Then I started wondering what a callback function actually is (despite using them for years and years and years). It's just a function, and it's the way it's called that makes it a callback in a sense. And that made me think of indirect addressing rather than direct addressing at the low level. But then, are virtual functions callbacks? Not really but they are called indirectly... I think it's time to go home. :)

          Regards, Rob Philpott.

          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
            #5

            No, it is just a function that gets called by the run-time library, framework etc. I have a feeling (vague memory) that you can tell the linker to use some other function as the starting point, but don't know why you would want to.

            OriginalGriffO 1 Reply Last reply
            0
            • R Rob Philpott

              I can't make up my mind.

              Regards, Rob Philpott.

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

              Probably not:

              Callback (computer programming) - Wikipedia[^]

              a callback, also known as a "call-after"[1] function, is any executable code that is passed as an argument to other code that is expected to call back (execute) the argument at a given time.

              So to be a callback function it would need to be passed as an argument to some other function, and I can't see any good reason to do that when you can call it directly or via a function pointer / function table that is set by the compiler / linker. That you can't call main at all except from a single point in your app kinda backs that up as well!

              "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

              R 1 Reply Last reply
              0
              • L Lost User

                No, it is just a function that gets called by the run-time library, framework etc. I have a feeling (vague memory) that you can tell the linker to use some other function as the starting point, but don't know why you would want to.

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

                Richard MacCutchan wrote:

                don't know why you would want to

                To annoy the next poor developer that has to work on your satan-spawned code ... :laugh:

                "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

                1 Reply Last reply
                0
                • R Rick York

                  Yes, it is, and it's not re-entrant.

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

                  R Offline
                  R Offline
                  Rob Philpott
                  wrote on last edited by
                  #8

                  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.

                  R G H 4 Replies Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Probably not:

                    Callback (computer programming) - Wikipedia[^]

                    a callback, also known as a "call-after"[1] function, is any executable code that is passed as an argument to other code that is expected to call back (execute) the argument at a given time.

                    So to be a callback function it would need to be passed as an argument to some other function, and I can't see any good reason to do that when you can call it directly or via a function pointer / function table that is set by the compiler / linker. That you can't call main at all except from a single point in your app kinda backs that up as well!

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

                    R Offline
                    R Offline
                    Rob Philpott
                    wrote on last edited by
                    #9

                    Yes, but I kind of expect the OS to call my method at the given time of the process starting up...? And the address is passed to the OS to be called back on, just through an extra layer of module EXPORTS etc. It's a pedant's dream this. I should move on.

                    Regards, Rob Philpott.

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

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

                      I couldn't agree more.

                      "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 Rob Philpott

                        I can't make up my mind.

                        Regards, Rob Philpott.

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

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

                        I, for one, like Roman Numerals.

                        OriginalGriffO T 2 Replies 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.

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

                          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 1 Reply Last reply
                          0
                          • R Rob Philpott

                            Yes, but I kind of expect the OS to call my method at the given time of the process starting up...? And the address is passed to the OS to be called back on, just through an extra layer of module EXPORTS etc. It's a pedant's dream this. I should move on.

                            Regards, Rob Philpott.

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

                            The OS doesn't call your main method at all - there are three places it can start: the MZ Stub (which for Windows apps will just print "this program cannot be run in MSDOS mode" and quit the app), the NE or (for more modern apps) the PE: Portable Executable - Wikipedia[^]. And EXE files (even old MSDOS 16 bit apps) don't call main immediately anyway, they do allocation and static initialisation before they are ready to start running the code you wrote!

                            "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

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

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

                              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!

                              "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
                              • 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.

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

                                Rob Philpott wrote:

                                C# ... is a language for wimps

                                To quote my elderly cat, "Fight me, bitch."

                                Software Zen: delete this;

                                R 1 Reply Last reply
                                0
                                • R Rob Philpott

                                  I can't make up my mind.

                                  Regards, Rob Philpott.

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

                                  It probably depends on the language, compiler, system, etc. As far as I know, in the languages I use, there nothing special about call-back functions, it's only about how an ordinary function is used. There's no reason to declare that no language will ever allow it. I seem to recall having a desire for a recursive main. : ) Just because.

                                  1 Reply Last reply
                                  0
                                  • G Gary Wheeler

                                    Rob Philpott wrote:

                                    C# ... is a language for wimps

                                    To quote my elderly cat, "Fight me, bitch."

                                    Software Zen: delete this;

                                    R Offline
                                    R Offline
                                    Rob Philpott
                                    wrote on last edited by
                                    #17

                                    It's how I express my intent daily to my ungrateful computer - I'm hooked, but its still the kid's soft play of computer languages. :-D

                                    Regards, Rob Philpott.

                                    1 Reply Last reply
                                    0
                                    • R Rob Philpott

                                      I can't make up my mind.

                                      Regards, Rob Philpott.

                                      Z Offline
                                      Z Offline
                                      ZurdoDev
                                      wrote on last edited by
                                      #18

                                      How dare you ask programming questions in the lounge?!! I'm outraged. :mad:

                                      Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

                                      R 1 Reply Last reply
                                      0
                                      • Z ZurdoDev

                                        How dare you ask programming questions in the lounge?!! I'm outraged. :mad:

                                        Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

                                        R Offline
                                        R Offline
                                        Rob Philpott
                                        wrote on last edited by
                                        #19

                                        Yeah, it's good to be reckless sometimes. Send me the codez and I'll go away. :)

                                        Regards, Rob Philpott.

                                        1 Reply Last reply
                                        0
                                        • OriginalGriffO OriginalGriff

                                          The OS doesn't call your main method at all - there are three places it can start: the MZ Stub (which for Windows apps will just print "this program cannot be run in MSDOS mode" and quit the app), the NE or (for more modern apps) the PE: Portable Executable - Wikipedia[^]. And EXE files (even old MSDOS 16 bit apps) don't call main immediately anyway, they do allocation and static initialisation before they are ready to start running the code you wrote!

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

                                          R Offline
                                          R Offline
                                          Rob Philpott
                                          wrote on last edited by
                                          #20

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