CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2003
    Posts
    41

    Whats wrong with void main(....)?

    Can anybody quickly explain to me why it is bad practice to do void main(int a, char**b) instead of int main(int a, char** b)? Where am I returning an int to anyway? Just a question thanks for your thoughts.
    Matthew Sanford

    "The man who doesn't read good books has no advantage over the man who can't read them."
    -- Mark Twain

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Whats wrong with void main(....)?

    Originally posted by msanford
    Can anybody quickly explain to me why it is bad practice to do void main(int a, char**b) instead of int main(int a, char** b)?
    Because it isn't valid ANSI C++, and never was valid ANSI C++. It's just that VC++ and a few others have held onto this incorrect syntax, so as not to break other code. On the other hand, many compilers will reject the void main() and will properly report it as an error.
    Where am I returning an int to anyway? Just a question thanks for your thoughts.
    At program termination, the exit() function is called from main() with the return value. If no return value is specified, then a "return 0;" is implied.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Take a look at this thread...

  4. #4
    Join Date
    Sep 2002
    Posts
    1,747
    Another particularly important problem with void main is that you will lower your potential for jobs. I for one have become so tied up recently with converting old demo projects to standard c++ so that they can compile for different machines on different compilers that I have made a conscious decision to not hire any potential developers who submit sample code with void main or ".h" standard headers, as these mistakes are occupying way too much of my time recently...
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  5. #5
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    you can't just do a find and replace? If you put a vc project in the root of c, then use find/replace with subfolders enabled?

  6. #6
    Join Date
    Sep 2002
    Posts
    1,747
    Here is an example for VC++ of the changes that occur when upgrading from the old ".h" libraries. Unfortunately, if you compound these code changes over many small projects which need to be integrated and include many of the other old header changes, your calculations equal "one big waste of time".
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  7. #7
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    thats no fun... at you will build up good copy and paste skills

  8. #8
    Join Date
    Mar 2003
    Posts
    41
    Thank you for your responses. I can see that while learning it may be easier to use void main. However it is incorrect and unprofessional to do this in code that others may rely on. Since standards are what make programs work together I for one will make a more consious effort to correct this flaw. Besides void is one more charecter than int.
    Matthew Sanford

    "The man who doesn't read good books has no advantage over the man who can't read them."
    -- Mark Twain

  9. #9
    Join Date
    Mar 2001
    Posts
    168
    This is what people in the 80s did to us.
    I remember a magazine (for the Amiga) showing that void main(void){} will result in much smaller code size than int main(int a, char **b){}

    Of course, things like that are largely irrelevant today. I don't even think that void main(void) generates smaller code on today's executables anymore.

  10. #10
    Join Date
    Jun 2002
    Posts
    1,417

    Smile

    VC++ 6.0 there is only one assembly instruction different -- int argc(...) with return 0 adds the instruction
    Code:
     00000	33 c0		 xor	 eax, eax
    What difference does that trivel thing make in a 1-meg or larger program ????

  11. #11
    Join Date
    Jan 2002
    Location
    TamilNadu, India
    Posts
    158
    Upon looking at this thread, I actually was thinking about a pertinent issue.

    How relevant is a c++ program size in today's computing environment?

    We talk of inline functions, object size, memory allocation in heap etc., But are they still relevant? Ofcourse a neatly written code is a maintainer's pleasure. But apart from this, are the above arguments/procedures/standards still needed?
    Muthu

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...basically you are right. Nowadays, most of the time you do not need to care much whether your application will take some bytes more than needed. However, many people here also know how it was several years ago, where all these things were limited and you had to save every byte you could.

    As for myself, although there is plenty of system resources available I still try to not waste unnecessary resources...

  13. #13
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374
    About memory economy, I think it has become less of an issue for small structures, but for large structure, programmer still have to be "size" conscious. For example, you won't be storing 12 copies of a 1280x1024 image, you'll store pointers or references to it instead. It's just common sense. But for smaller structures like strings or ints? nah... why bother.
    Martin Breton
    3D vision software developer and system integrator.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured