Thread: illegal break

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    If the compiler doesn't setup what happens before main() then who does?
    I thought the compiler creates everything from the point when the OS starts the program?
    Yes, but the compiler is not usually creating the code that goes before main as part of building the application itself. It is, rather, a part of the C library, which may not even be part of the compiler distribution (for example gcc doesn't normally come with glibc, but it does link to glibc).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Yes, but the compiler is not usually creating the code that goes before main as part of building the application itself. It is, rather, a part of the C library, which may not even be part of the compiler distribution (for example gcc doesn't normally come with glibc, but it does link to glibc).

    --
    Mats
    In that case, I guess the compiler could create its own main() with the initialization stuff, then call the real main() (renaming it first of course).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    In that case, I guess the compiler could create its own main() with the initialization stuff, then call the real main() (renaming it first of course).
    It could. But what gcc does is call __main() inside main, because that solves the same problem - and saves renaming main and potentially causing problems because of collisions with the renamed main.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    But I mean, why can't they do the global construction before calling main()?
    Some runtime environments may not support it. Rather than restrict C++'s applicability to runtimes where this kind of initialization is possible, the standard writers left it open-ended by specifying that main() itself could contain global initialization code. On some platforms there might not be anywhere else to put it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Wait.. really? Can you give an example of a platform that is unable to use a different entry point to perform initialization and then call main()?

    For example, Visual C++ uses the entry point mainCRTStartup from their runtime library as the actual entry point, and mainCRTStartup calls main (or wmain). I don't see how something like this wouldn't be possible on every platform...

    Or why couldn't a compiler behave as if an implicitly defined entry point existed in the code? For example:
    Code:
    int __real_entry_point() {
       // determine commandline parameters
       // do initialization stuff
       // etc...
    
       return main(argc, argv);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM