Thread: Size of Main() ?

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    Size of Main() ?

    In general is it better to keep main() as small as possible? is like one page too big? what about in larger projects with thousands of lines? this is just in relation to my query about getting my work more organised.
    the question was prompted after reading the development process thread, where he says 'main is getting a bit big' yet to me seemed fine.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    In general, you should cut some pieces out if they are used repetitively, or you might want to use them in another project. I don't think you really need to worry about the literal length of main() too much. At the end, I think its personal taste.

    And about the development process thread, he's talking *in general*. Even though it doesn't look too big now, it usually would be in a real project.
    I might not be a pro, but I'm usually right

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    printf("%u\n", sizeof(main));

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the rule of 1 page could be applied to any function as well as main
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The discussion on "when is a function too large should apply to main as well as any other function". Although in general, I prefer to make main really short, and put the "overall function of the application" into another function that then does all the relevant work and calls other functions from there. If you do that from the outset, there is less risk that "other crap" ends up in main.

    Of course, the same rules apply to main as any other function: It should do one thing, and one thing only - which in this case is "start and finish the program", so ideally, main should be something like this:
    Code:
    int main()
    {
       Initialize();
       result = DoStuff();
       CleanUp();
       return result;
    }
    Of course, small applications can be pretty much "just main", but as soon as it's not, then the above type of model is ideal.

    Edit: And sizeof(main) will either not compile, or return the size of a pointer to a function, neither of which is particularly useful.

    --
    Mats
    Last edited by matsp; 02-06-2008 at 05:27 AM.
    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
    Banned
    Join Date
    Nov 2007
    Posts
    678
    matsp, i do exactly like you have shown. my main always calls a few functions and exits.
    otherwise a bigger main confuses me.

    >sizeof(main)
    i was just joking there.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    neat one with the sizeof() hoho, you win a bottle of our second least expensive champagne!
    Going on to what was mentioned by Matsp, i also read in a tutorial to always keep a function within one screen length wherever possible, its probably just my rubbish coding but is that always 'on'?
    Also is it good just to call a function that executes maybe one line of code? i understand its better that way becasue you can forget about it, but is it wasteful in any way? what does it cost calling functions?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, many people advise against a function of one line of code. But for a few lines, a function can do well.
    If a function is sufficiently small and not called very often the compiler might just inline it and eliminate the function call overhead.
    Still, overhead is not something you really have to be worried about.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks rogster001! i am already drunk! hic! hic!

    i don't mind writing one line functions. in c++ they are even efficient. use inline keyword.
    a classic example would be:
    Code:
    inline bool equals(const char *s1, const char *s2)
    {
        return (strcmp(s1, s2) == 0);
    }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, there's nothing inheritably wrong with them, but some people don't like them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The cost of a function is about a penny each :-)

    The general rule of optimization is "don't optimize things until you know it's part of the 'slow' bits of your software". Since it's often much easier to make a tiny function work right than to make a large function work right, it's a good idea to make small functions for that reason.

    When it comes to the cost of actually calling a function, it depends on many things. Most compilers support automatic inlining, where the compiler itself decides "if it's worth it" to inline a function, so many times, writing a small function isn't actually costing anything, because the compiler inlines the function. In gcc, I've seen pretty huge chunks of code inlined if the function is called only once.

    The most obvious case of "non-inlinable" small functions are:
    1. functions defined in another source file - the compiler must be able to see the source code of the function to inline it.
    2. functions called via function pointers. (This also applies to C++ virtual methods)

    As to the recommendation for function size, the MAX size is about a page [now, the next discussion is if that's a page on a 19" high-res monitor, or a 80x25 text-display with two lines of "editor info"...].

    There are situations when really long functions make more sense than splitting, for example a function that takes 200 values into a switch statement and doing one simple thing for each case (like calling a function or setting a variable), then you can probably get away with making that one function.

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

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    use function pointers then!

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hope you're sarcastic.
    The point was not to use them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    printf("%u\n", sizeof(main));
    Edit: And sizeof(main) will either not compile, or return the size of a pointer to a function, neither of which is particularly useful.
    Not to mention that sizeof() doesn't return an unsigned int, so either a cast or the C99 %z format specifier would need to be used . . .

    And I think it would compile, and return the size of a function pointer, pointing to main. Which would likely be the same size as any other pointer: 2 (for 16-bit), 4 (for 32-bit), or 8 bytes (for a 64-bit computer). Of course, main() might be different, because main() is unique and strange. (For example, you can't call it in C++.)

    Well, that's what I thought . . . until I tested it.
    Code:
    $ cat sizeofmain.c
    #include <stdio.h>
    
    void func(void) {}
    
    int main() {
        void (*fp)(void) = func;
        printf("main: %lu\n", (unsigned long)sizeof(main));
        printf("func: %lu\n", (unsigned long)sizeof(func));
        printf("fp  : %lu\n", (unsigned long)sizeof(fp));
        printf("cast: %lu\n", (unsigned long)sizeof((int (*)(void))main));
        return 0;
    }
    $ ./sizeofmain 
    main: 1
    func: 1
    fp  : 8
    cast: 8
    $
    I'm working on a 64-bit computer at the moment, as you may have guessed. However, I have no idea why the sizeof a function is 1 and the sizeof a function pointer is 8 (or whatever). Any suggestions as to why this is?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What's that about you can't call main?
    You can, but it's probably undefined.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  2. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  3. Main Declaration error
    By starkhorn in forum C++ Programming
    Replies: 11
    Last Post: 06-22-2005, 09:04 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  5. void main
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-29-2002, 07:08 PM