a simple doubt!

This is a discussion on a simple doubt! within the C Programming forums, part of the General Programming Boards category; Hello, we all know that in C/C++ execution of program begins with main() as it's the entry point. But since ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    a simple doubt!

    Hello,
    we all know that in C/C++ execution of program begins with main() as it's the entry point. But since i started learning VC++ its written in every book that global application object gets created and then WinMain() gets called.
    So i wanted to ask the same question whether in C/C++ also all global variables get created first and then main() gets executed? or first main() is called and then all global variables are called?


    Also what exactly is meant by top down programming and bottom up programming? i mean C is top down and C++ is bottom up! why so?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, they're "created" at compile time. That'd make it before main() gets called.

    The two terms, top-down and bottom-up are far easier to visualize graphically (and object oriented IMO). For example, say you have 5 modules, A, B, C, D, E. Which has a dependancy tree of:

    Code:
       A
      / \
     B   C
        /
       D
      /
     E
    That is, A uses B & C. C uses D which uses E.

    The general idea of top-down is to implement the "logic" modules first, ie those at the top. For example, You'd implement A then B & C, then D, E. At which point you ask, but how do I implement A if I haven't implemented B? Well you make a stub of B that does nothing really, just returns desired results.

    Bottom-up goes the other way, say from E, D, B & C then A. Where the logic modules are implemented last (terrible for testing the logics of your design). But instead of using stubs, drivers are used. There is also a hybrid sandwich approach, pretty straight forward how that'd work .

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Perhaps your question is really only about C++, and not about C at all...

    In C, none of your code is ever executed before the first line of code in main.

    In C++, all global objects are constructed some time prior to the first line of code in main. That's all you need to know, apart from perhaps being aware of the 'static initialisation order' problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by iMalc View Post
    In C, none of your code is ever executed before the first line of code in main.
    That's not entirely true. Globals are still initialised to 0 unless defined with an initialisation value.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, this is getting a bit detailed, but in essense, in C there is a little bit of code that runs before main, but it does nothing with any of the data/code you have created - it splits the command line arguments and such things, so that you can use argc, argv, and does some other preparations (for example setting up some runtime library stuff so that when you call malloc, printf, etc the first time, they are already ready to work - you don't need to call "initialize_heap" or "open_std_files" for example - those are just names I made up, by the way). But as far as you need to know in most cases, the first thing that happens in a C program is the first line of what you wrote in main. I guess a good analogy is that if you go to the theatre, the actors didn't just appear on stage when the curtain went up - but that's the first you see of them.

    In the case of C++, some time before your code in main [1], the construction of global objects will be performed. In all other aspects, the first thing that happens is main.

    WinMain is a different name for main(), but otherwise the effects/process is the same. My take on "WinMain" is that in the first version of Windows compilers, WinMain was called from main() - but I'm not entirely sure if that is the explanation, or something else is.

    [1] I'm very careful to word this as "before your code in main", because some compilers put the construction code inside main, but of course, it is not YOUR code. In other compilers, it happens before main is called. It really makes little difference which way it is done.

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

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    That's not entirely true. Globals are still initialised to 0 unless defined with an initialisation value.

    QuantumPete
    But that's actually done as part of the process startup (by the "loader" functionality in the OS), which is BEFORE any of the code in your application is being executed - that is if the data section (which is the part that holds global variables) isn't a huge lump of zeros in itself. Most modern OS's do not store all global data in the executable file, so you would have "holes" where the uninitialized (to be set to zero) lives. If the data is all zero, then the OS would point to a dummy all-zero page and then say "duplicate on write", so the first time you write to that page, it gets allocated to the writing process - until then, there's one page for many different virtual addresses, and thus saving memory.

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

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Thanks now all things are getting cleared in my mind

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by QuantumPete View Post
    That's not entirely true. Globals are still initialised to 0 unless defined with an initialisation value.

    QuantumPete
    Yes int x=42; is "code", but I was intending a narrower scope when I said "code". You won't get any if-statements running prior to main in C for example.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21