Thread: Program Entry Point

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Program Entry Point

    Whenever a program is compiled successfully and run, is the entry point (starting point) always going to start at sometype of 'main' function? Like in a console app, the starting point is always int main(), correct? And in Windows it's always Winmain()? What I'm saying is, when you run a program it will always look in the main function, then go on from there, is this correct in saying that?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    From the perspective of C++, yes.
    My best code is written with the delete key.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I believe so, wrt C++. Write a program without any main() and see what errors it says.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well from the perspective of hosted systems, yes.
    Free-standing implementations are a whole new ball game.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    There are some internal C code (CRT) that executes before your code. There is a function in that internal codes that call your main() or winmain() or any oher entery point.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    That may be true of microsoft compiler. But not of any other compilers.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Weird. I didn't know that was purely MS.

    Hey Mario, I found some code that messes up that color coder program.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The C Runtime Libraries are not an imposition. Compilers may or may not choose to implement/use them.

    Anyways, fix it then! :G
    (I completely forgot to keep checking on that)

    EDIT: were on earth are the smileys?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Constructors of static objects at file scope will be invoked before main() is called.

    Code:
    #include <iostream>
    
    class X
    {
        public:
           X() {std::cout << "1\n";};
           void something() {};
    }
    
    X x;
    
    int main()
    {
         std::cout << "2\n";
         x.something();
    }
    will print 1 then 2, rather than the reverse.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    >>That may be true of microsoft compiler. But not of any other compilers.
    C/C++ compiler and linker manage the program in a way that it calls a function in one default lib or dll. Then that function calls your main().
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> Anyways, fix it then! :G

    I didn't write it!


    >> Constructors of static objects at file scope will be invoked before main() is called.

    Interesting. I didn't know that!

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by siavoshkc
    C/C++ compiler and linker manage the program in a way that it calls a function in one default lib or dll. Then that function calls your main().
    Compilers for hosted implementations can manage the program however they damn well please, as long as the effect remains the same. They could, for example, take your definition of the main function and rewrite that to include some startup code and generate the argc and argv "arguments" somehow. In standard C++, main() is not callable, nor can you take its address. That permits the compiler such games.

    I don't know any compiler that does this. It's just a possibility. For what it's worth, the MS compilers pull in the code from crt0.c and call that function for startup, which in turn calls main(), wmain(), WinMain() or wWinMain(), depending on the settings.

    I think the equivalent in GCC is in crtbegin.o, but it might also be ld-linux-x86-64.so.2 (my system only - this is architecture-specific stuff), crt1.o, crti.o, libgcc.a, libgcc_s.a, crtend.o or crtn.o - those are all the additional files GCC automatically links.

    Details for older kernels:
    http://asm.sourceforge.net/articles/startup.html
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    So as I understood, in standard the entery point of program is main() with no alternatives. But for making the things more simple compiler implementors decided to change it and make another entery point to do the system initializations and then call the main(). Correct?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    So as I understood, in standard the entery point of program is main() with no alternatives. But for making the things more simple compiler implementors decided to change it and make another entery point to do the system initializations and then call the main(). Correct?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not quite. The C and C++ standards say that the program, as written by the user, must start execution at the main() function.
    They do not say that the physical, completed program must execute this as the first instruction. In fact, that would be quite unrealistic. Take the Linux model for example. There, you create a new process by calling fork(). However, fork() doesn't load a new application. It only duplicates the current process, and then in both processes returns from fork() - in the parent process, it returns the new process id, in the child process it returns 0. This is for the client to distinguish the two. But they still share the same code, the same file handles, and all variables are, for the moment, in the same state. (Hoever, they are copy-on-write, so modifying them in one process doesn't affect the other.)
    It's not until one process calls exec*() that something new happens. This call goes into the kernel and eventually replaces the whole program code by something different. This means that the kernel first cleans out the old state, sets up a new state, then loads the program code. Executable formats in Linux are flexible, but the ELF format is the most common. It contains (just like PE does) in its header a starting address. The kernel, after loading the code and setting up the application state, just jumps to that address.
    It is up to the compiler what it places at that address. And as it's totally invisible to the user of C or C++, the language standards having nothing to say about it either. (With one exception: the C++ standard requires the initialization of global objects, so it effectively requires that the compiler does something before giving main() a chance.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with Program!
    By afnitti in forum C Programming
    Replies: 9
    Last Post: 04-15-2009, 08:06 PM
  2. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  3. Class warfare
    By disruptivetech in forum C++ Programming
    Replies: 13
    Last Post: 04-22-2008, 01:43 PM
  4. Replies: 2
    Last Post: 06-18-2004, 08:08 PM
  5. Results for the Encryption Contest -- June 23, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-07-2002, 08:04 AM