Thread: college needs help again...ya diggg

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The only reason there would be stack corruption based on the parameters to main is if it's a "callee clean stack" function. In x86 that isn't the common case, although Windows __stdcall calling convention does that. However, main is not a __stdcall function, so it's the responsibility of the caller to clean up the stack.

    Other architectures may vary of course, depending on what the functionality that different instructions offer. But C-code basicly requires caller-stack-cleanup because of the ability to take variable number of arguments - the called function may not know [from a static analysis standpoint] the number of arguments, so the clean-up will need to be in the calling code for these functions at the very least.

    --
    Mats

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So even in C int main() is the same as int main(void)? Strange, I could have sworn that someone said they were different. Actually, I thought it was you.
    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.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Here's the "int main()" vs. "int main(void)" thread.

    http://cboard.cprogramming.com/showthread.php?t=88714

    Edit: The only dispute is regarding the situation in C - in C++ they are definitely the same, since in C++ the absence of an argument in a function declaration means unambiguously that there aren't any.
    Last edited by robatino; 08-29-2007 at 03:26 PM.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oh, it wasn't matsp. Sorry. Mistaken identity.

    The only dispute is regarding the situation in C - in C++ they are definitely the same, since in C++ the absence of an argument in a function declaration means unambiguously that there aren't any.
    I don't understand.

    In C, int main() is okay, because it means that main()'s arguments are unknown, and when it's passed argc and argv, the code won't toast the stack because, not knowing if it would be passed arguments or not, it made allowances for being passed arguments; whereas int main(void) is not okay, because the code could assume that it's not being passed any arguments, and mess up the stack when it is passed argc and argv.

    Am I right so far? brewbuck's post supports me, as far as I can tell.
    Quote Originally Posted by brewbuck
    In case you're wondering... The reason you must not declare main(void) is because of parameter passing conventions. Under most ABI's, C parameters are evaluated and passed right-to-left, with the CALLER of the function being responsible for managing the stack. However it is conceivable that on some platform somewhere it is the CALLEE's responsibility to clean the stack.

    The problem is, if the function is specifically declared to take a void argument list, the compiler (rightly) assumes that it will never be passed any arguments, so it will not produce the code necessary to fix the stack if arguments had been passed. Then, when the main(void) gets invoked, the C library DOES pass arguments (argc, argv), but main is not aware of this. So when main() returns, the stack becomes corrupt and things go wrong.
    Okay. So when prototypes like int fputc() (that is, prototypes without parameters) were excluded from C++, int fputc() became the same as int fputc(void) -- that is, the function takes no arguments, rather than "this function takes an unknown number and type of arguments".

    Wouldn't functions, such as main(), be the same way? int main() in C has suddenly become int main(void) in both C and C++. int main(void) is not good in C, so why should it be good in C++?

    There must be a hole in my reasoning somewhere. Feel free to point it out.

    [edit] It's probably that empty-function-prototypes-are-the-same-as-empty-function-declarations thing. [/edit]
    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.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A quick glance through that thread indicates that int main() is not valid in C because the C99 standard says so.

    The C++98 standard says int main() is valid, and so we're done. C++ obviously has different requirements that force implementations to make it work.
    Last edited by Daved; 08-29-2007 at 05:36 PM.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you do a prototype for a C function in C++, you should use
    Code:
    extern "C" int somefunction();
    , which tells the compiler to use "classic C" prototype interpretation, rather than C++ interpretation.

    Of course, it doesn't really matter if you use main() or main(void) in the case where the callee cleans up the stack - it's impossible for the callee to know how many arguments where passed - there is also
    Code:
    int main(int argc, char **argv, char **envp);
    , where three arguments are passed.

    --
    Mats

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, good to know. Sorry, everyone, for hijacking the thread and all that.
    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.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Daved View Post
    A quick glance through that thread indicates that int main(void) is not valid in C because the C99 standard says so.
    The C99 standard explicitly mentions "int main(void)" but says nothing about "int main()".

    http://c0x.coding-guidelines.com/5.1.2.2.1.html

    Of course in C++ there's no need for "void" to indicate the absence of arguments. I think someone once called the use of "void" for this purpose an "abomination" that was only necessary because of the old-style K&R function declarations in C, which are not allowed in C99 or C++.

  9. #24
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by robatino View Post
    Of course in C++ there's no need for "void" to indicate the absence of arguments. I think someone once called the use of "void" for this purpose an "abomination" that was only necessary because of the old-style K&R function declarations in C, which are not allowed in C99 or C++.
    Several people in fact.. most notably, Bjarne Stroustrup said this about (void) between function parenthesis.
    http://www.parashift.com/c++-faq-lit....html#faq-29.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. College residence - Campus or commute?
    By holden in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-17-2004, 03:54 AM
  2. Should I go to a state college and be 20k in debt, or a private college and be 90k in
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-10-2003, 08:22 PM
  3. University versus college, which should I do?
    By Terrance11 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-02-2003, 11:19 PM
  4. Which college should I go to?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-13-2003, 10:06 PM
  5. College or No College :: Future
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 09-25-2002, 03:48 PM