Thread: How is main() called?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    57

    How is main() called?

    Hi,
    Was wondering how to locate resources on the net that tell me the internals of C, how the main() is called? I tried googling with "C internals", "how main() is called" and the like but in vain.

    Any pointers on the answer and some resources on the net will be useful.

    Thanks,
    Anoop.
    Intelligence: Finding an error in a Knuth text.
    Stupidity: Chasing that $2.56 cheque you got.

    Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live.

    If Java had true Garbage collection, most programs would delete themselves upon execution.

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    What do you mean, "how main() is called"?

    If you mean how you need to write it in code, use either:-
    Code:
    int main(void)
    {
    //code goes here
    }
    or
    Code:
    int main(int argc, char **argv)
    {
    //code goes here
    }
    Depending on whether you need any information from the command line or not.

    If you mean "how/why does an operating system call main()", then what do you expect it to do? You could argue that it should just run programs starting at the first bytes in the file, but C/C++ are structured, function-based languages and the OS also needs to know certain information about the program before running it, i.e. how much memory it thinks it will need, which parts of it are code and which are data, etc.

    Under Windows, main(...) (Or WinMain(...) for GUI-based programs) is exposed to the OS via a table usually towards the beignning of the program file known as the "export table", which tells Windows exactly where in the program to begin execution.

    Older, small programs referred to as .COM files usually ran from the beginning of the file, but certain assumptions were made on how they operate and thus they were only suitable for small utilities.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Main is called by the assembly startup file created by your compiler. This assembly startup inits the needed variables and prepares your program to be run.

    Then:

    call main

    You can disassemble the startup.obj file with a disassembler or with the one that came with your dev environment.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    For HP Unix:

    C compilers automatically link a _start module that is found in crt0.o -
    _start calls _crt_open to create stdout for example. There are some other specialty
    startup modules that you can link to instead of crt0.o.

    How the compiler does this on your system is installation dependent.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    You could cause to main() produce a backtrace and see for your self who called it. For a glibc based system it could look like this:

    Code:
      #include <stdlib.h>
          #include <execinfo.h>
          
          int main(int argc, char *argv[]) {
            void *apRaw[16];
            char **pasReadable;
            size_t iSize;
            size_t i;
           
            iSize= backtrace(apRaw, 16);
            pasReadable= backtrace_symbols(apRaw, iSize);
          
            printf("Backtrace, %zd calls:\n", iSize);
            for (i= 0; i < iSize; i++)
          	printf("%s\n", pasReadable[i]);
           
            free(pasReadable);
      return 0;
          }
    edit: I don't know why the the board messes up the code like this. sorry.
    Last edited by Nyda; 11-05-2004 at 12:20 PM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Nyda
    edit: I don't know why the the board messes up the code like this. sorry.
    Probably because you use tabs instead of spaces.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    8
    Or, if you have a recent copy of Visual Studio, have a look in \Vc7\crt\src.

    crt0.c is responsible for calling your application's entry point, while the other crt*.c files contain various support functions.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by itsme86
    Probably because you use tabs instead of spaces.
    No, I don't ever use tabs except for Makefiles. Actually every time I edit a post, code blocks get worse, even when I don't change anything in the code block. Maybe that java component editor is broken on *nix boxes?
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The code box code is broken in that aspect. Any edit of your post will cause the code to be left-justified which is quite annoying. It has nothing to do with tabs or spaces.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    Thanks a lot.

    I wanted to know how is main() called by the compiler. I guess I should have mentioned the OS I am using. Fedore Core 2. I will try the piece of code Nyda has given. Thanks a lot for that.

    Any pointers on any resources which can help me understand it more and better, on the net?

    thanks,
    Anoop.
    Intelligence: Finding an error in a Knuth text.
    Stupidity: Chasing that $2.56 cheque you got.

    Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live.

    If Java had true Garbage collection, most programs would delete themselves upon execution.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I wanted to know how is main() called by the compiler.
    Read above.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    That was a reply to SMurf who wanted t o know what exactly I had meant by that. As I said, I would try out Nyda's code. I did. Now I am hunting for resources on the net on those functions, backtrace() and backtrace_symbols(). I did look it up in the execinfo.h but I am sure I can locate detailed info on it.


    The one answer that I have not got is, if I can get any info on the net over the internals of calling mechanisms. Or should I objdump the executable and try and learn more about it? Is that the right way to proceed?


    Thanks a lot for any pointers.

    Anoop.
    Intelligence: Finding an error in a Knuth text.
    Stupidity: Chasing that $2.56 cheque you got.

    Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live.

    If Java had true Garbage collection, most programs would delete themselves upon execution.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Main is called from the startup module. Its just a simple call main. In assembly a call statement signals the CPU to do several things and then jump to a new section of code.

    For specifications about the calling mechanism consult the Intel IA32 and IA64 tech refs.
    They break the whole process down step by step.

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    Thanks a lot
    Intelligence: Finding an error in a Knuth text.
    Stupidity: Chasing that $2.56 cheque you got.

    Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live.

    If Java had true Garbage collection, most programs would delete themselves upon execution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM
  3. Int Main or Void Main?
    By FromHolland in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2003, 04:29 PM
  4. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM
  5. what to do with the main window
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 08:58 PM