Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: Big Deal with returning int (http://www.velocityreviews.com/forums/t267850-re-big-deal-with-returning-int.html)

Kelsey Bjarnason 06-29-2003 09:23 AM

Re: Big Deal with returning int
 
[snips]

On Sun, 29 Jun 2003 07:18:14 +0100, John Harrison wrote:

>> I have seen so many pointing out "main" should explicitly return "int".
>> Beside, the language spec or committee, or some guru said so, what is a

> BIG
>> deal with it ?

>
> I really have no idea. As you say it is pointed out time and time again
> here, but no-one seems to be able to explain why. You'd think that with the
> frequencey that this is pointed out, someone would also supply an
> explaination.
>
> Furthermore even though main must be declared to return an int, you don't
> actually have to return one! Its the only function that gets this special
> treatment. To me its bizarre, and since I have never seen an explaination I
> assume its has none.
>
>> What difference does it make if "void main" rather than "int
>> main" ?

>
> Personally I wouldn't take the presence of void main as evidence against a
> books quality, I would look to more important issues. void main is a
> completely trivial issue which like many trivial issues (indentiation style
> for instance) gets some people hot under the collar.


Except that void main isn't a trivial issue; worse, by getting such a
_simple_ issue wrong, the authors instantly lose any credibility they may
have had when it comes to dealing with hard things.

As to _why_ int main is important...

Well, first, that's how the language is defined, which means using int
main is the only assured way to have things work. If one only ever uses a
compiler which accepts void main, one may never notice this directly, but
C++ isn't about code that runs on one compiler, it's about portable code.

That said... here's a possible implementation, in terms of the details of
the startup code:

First, the "init" module is run, which sets up various things. It calls
main, which does its thing, returns, and the init code rips the return
value and hands it back to the universe.

However... this implementation is a little odd; it doesn't use a stack.
Instead, space is reserved at the head of the function. Thus, the init
code looks something like this:

.init proc
load rega <- .main ; address of main
add rega, 4 ; size of int
sub rega ; call main

Now, since you've told your compiler that main returns void, it compiles
as you told it to. Thus, the code looks lke this:

.main proc
; code starts here

Oops, we have a problem - there's _supposed_ to be space there for a
return value - an int. The startup code *knows* main returns an int, so
it automatically adds the size of one to the jump address. You, however,
have goofed and produced a broken main, so what is actually executed is
not the start of the code, but whatever happens to be 4 bytes in from that
point, which is almost guaranteed _not_ to do the right thing. Never mind
your app failing to return a proper value, your app may not run _at all_,
crashing at or very near to this point. Or it may run, but executing a
competely different set of instructions, with unpredictable results.

Fundamentally, it comes down to this: void main introduces a point of
failure which is both completely avoidable and _trivial_ to avoid; why,
then, risk failure?

--
http://rkc.silversapphire.com
Managed Migration from Windows to Linux



John Harrison 06-29-2003 10:39 AM

Re: Big Deal with returning int
 

"Kelsey Bjarnason" <kelseyb@xxnospamyy.lightspeed.bc.ca> wrote in message
news:pan.2003.06.29.09.23.22.840218@xxnospamyy.lig htspeed.bc.ca...
> [snips]
>
> On Sun, 29 Jun 2003 07:18:14 +0100, John Harrison wrote:
>
> >> I have seen so many pointing out "main" should explicitly return "int".
> >> Beside, the language spec or committee, or some guru said so, what is a

> > BIG
> >> deal with it ?

> >
> > I really have no idea. As you say it is pointed out time and time again
> > here, but no-one seems to be able to explain why. You'd think that with

the
> > frequencey that this is pointed out, someone would also supply an
> > explaination.
> >
> > Furthermore even though main must be declared to return an int, you

don't
> > actually have to return one! Its the only function that gets this

special
> > treatment. To me its bizarre, and since I have never seen an

explaination I
> > assume its has none.
> >
> >> What difference does it make if "void main" rather than "int
> >> main" ?

> >
> > Personally I wouldn't take the presence of void main as evidence against

a
> > books quality, I would look to more important issues. void main is a
> > completely trivial issue which like many trivial issues (indentiation

style
> > for instance) gets some people hot under the collar.

>
> Except that void main isn't a trivial issue; worse, by getting such a
> _simple_ issue wrong, the authors instantly lose any credibility they may
> have had when it comes to dealing with hard things.


Maybe they are just concentrating on the hard things. If they get the hard
things right then I wouldn't worry about the use of void main, that would be
perverse. And to dismiss all books that use void main is prejudiced. Sure
void main is an error, but I would still maintain its a trivial one.

>
> As to _why_ int main is important...
>
> Well, first, that's how the language is defined, which means using int
> main is the only assured way to have things work. If one only ever uses a
> compiler which accepts void main, one may never notice this directly, but
> C++ isn't about code that runs on one compiler, it's about portable code.
>


That just restates what I said earlier, int main is standard, and we should
use it for that reason. It doesn't explain why void main is non-standard.

> That said... here's a possible implementation, in terms of the details of
> the startup code:
>
> First, the "init" module is run, which sets up various things. It calls
> main, which does its thing, returns, and the init code rips the return
> value and hands it back to the universe.
>
> However... this implementation is a little odd; it doesn't use a stack.
> Instead, space is reserved at the head of the function. Thus, the init
> code looks something like this:
>
> .init proc
> load rega <- .main ; address of main
> add rega, 4 ; size of int
> sub rega ; call main
>
> Now, since you've told your compiler that main returns void, it compiles
> as you told it to. Thus, the code looks lke this:
>
> .main proc
> ; code starts here
>
> Oops, we have a problem - there's _supposed_ to be space there for a
> return value - an int. The startup code *knows* main returns an int, so
> it automatically adds the size of one to the jump address. You, however,
> have goofed and produced a broken main, so what is actually executed is
> not the start of the code, but whatever happens to be 4 bytes in from that
> point, which is almost guaranteed _not_ to do the right thing. Never mind
> your app failing to return a proper value, your app may not run _at all_,
> crashing at or very near to this point. Or it may run, but executing a
> competely different set of instructions, with unpredictable results.


There is no reason that the compiler couldn't perform the necessary
tranformation to void main, silently adding space for an int return. I think
you are confusing the implementaion of a language with its definition. I
can't see any reason why

void main()
{
}

couldn't have been defined as entirely equivalent to

int main()
{
return 0;
}

(for instance). Doing so would have validated common practise (which is no
bad thing), but it wasn't for reasons I still don't understand.

>
> Fundamentally, it comes down to this: void main introduces a point of
> failure which is both completely avoidable and _trivial_ to avoid; why,
> then, risk failure?


Are you really saying that allowing void main would put such a strain on
compilers that they could not produce correct code? I don't think so.

>
> --
> http://rkc.silversapphire.com
> Managed Migration from Windows to Linux
>


john




Rolf Magnus 06-29-2003 11:59 AM

Re: Big Deal with returning int
 
John Harrison wrote:

> Maybe they are just concentrating on the hard things. If they get the
> hard things right then I wouldn't worry about the use of void main,
> that would be perverse. And to dismiss all books that use void main is
> prejudiced. Sure void main is an error, but I would still maintain its
> a trivial one.


I see it a bit different. If an author uses void main, that shows that
he doesn't know C++ very well. This gives not much trust in the rest of
the book.

> There is no reason that the compiler couldn't perform the necessary
> tranformation to void main, silently adding space for an int return. I
> think you are confusing the implementaion of a language with its
> definition. I can't see any reason why
>
> void main()
> {
> }
>
> couldn't have been defined as entirely equivalent to
>
> int main()
> {
> return 0;
> }
>
> (for instance). Doing so would have validated common practise (which
> is no bad thing), but it wasn't for reasons I still don't understand.


It's quite interesting that you call "void main" common practise, since
it actually never was allowed. So you think the rule should be changed,
because nobody follows it anyway?

>> Fundamentally, it comes down to this: void main introduces a point of
>> failure which is both completely avoidable and _trivial_ to avoid;
>> why, then, risk failure?

>
> Are you really saying that allowing void main would put such a strain
> on compilers that they could not produce correct code?


Actually, that may be the case. Or a compiler may choose to not produce
any code at all since the source has an error.

> I don't think
> so.
>
>>
>> --
>> http://rkc.silversapphire.com
>> Managed Migration from Windows to Linux
>>

>
> john



John Harrison 06-29-2003 12:19 PM

Re: Big Deal with returning int
 
>
> It's quite interesting that you call "void main" common practise, since
> it actually never was allowed. So you think the rule should be changed,
> because nobody follows it anyway?


You only have to read here for a while to see that it is common practise.

>
> >> Fundamentally, it comes down to this: void main introduces a point of
> >> failure which is both completely avoidable and _trivial_ to avoid;
> >> why, then, risk failure?

> >
> > Are you really saying that allowing void main would put such a strain
> > on compilers that they could not produce correct code?

>
> Actually, that may be the case. Or a compiler may choose to not produce
> any code at all since the source has an error.
>


That's a gem of a circular argument. void main is illegal, therefore the
compiler needn't compile it, therefore we can't allow void main.

What I am saying (in contrast to Kelsey) is that there is no technical
reason that void main could not be part of standard C++. What ever reasons
the C++ committee chose to disallow void main (I suspect it was just
tradition) were not to do with the infeasibility of having void main as part
of C++.

john



Rolf Magnus 06-29-2003 01:43 PM

Re: Big Deal with returning int
 
John Harrison wrote:

>>
>> It's quite interesting that you call "void main" common practise,
>> since it actually never was allowed. So you think the rule should be
>> changed, because nobody follows it anyway?

>
> You only have to read here for a while to see that it is common
> practise.


Common practise for beginners, since those are mostly the ones asking
questions together with example code that contains "void main". I guess
that's mostly the fault of all the books that use it too. Btw, note
that the most recommended books, i.e. the books that are considered
best never return void from main (and no, that's not the reason why
they are considered best).

>> >> Fundamentally, it comes down to this: void main introduces a point
>> >> of failure which is both completely avoidable and _trivial_ to
>> >> avoid; why, then, risk failure?
>> >
>> > Are you really saying that allowing void main would put such a
>> > strain on compilers that they could not produce correct code?

>>
>> Actually, that may be the case. Or a compiler may choose to not
>> produce any code at all since the source has an error.
>>

>
> That's a gem of a circular argument. void main is illegal, therefore
> the compiler needn't compile it, therefore we can't allow void main.


What I actually wanted to say is that void main is illegal, therefore we
must not use it in our programs. What you seem to say is that "we" use
void main in our programs, therefore it must be made legal.

> What I am saying (in contrast to Kelsey) is that there is no technical
> reason that void main could not be part of standard C++. What ever
> reasons the C++ committee chose to disallow void main (I suspect it
> was just tradition) were not to do with the infeasibility of having
> void main as part of C++.


I wonder why it's so important for you to be able to write "void main".
What exactly doesn't work if main returns int? I don't really see a
need to allow it.
Btw, C does allow "void main" as an implementation defined extension in
the C99 standard.


John Harrison 06-29-2003 02:00 PM

Re: Big Deal with returning int
 

"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:bdmqou$6fo$05$1@news.t-online.com...
> John Harrison wrote:
>
> >>
> >> It's quite interesting that you call "void main" common practise,
> >> since it actually never was allowed. So you think the rule should be
> >> changed, because nobody follows it anyway?

> >
> > You only have to read here for a while to see that it is common
> > practise.

>
> Common practise for beginners, since those are mostly the ones asking
> questions together with example code that contains "void main". I guess
> that's mostly the fault of all the books that use it too. Btw, note
> that the most recommended books, i.e. the books that are considered
> best never return void from main (and no, that's not the reason why
> they are considered best).


I think the book argument has some validity when the book is about C++. A
book about C++ obviously should be written by an author who knows the
language well. But in a book on algorithms, or graphics or networking or how
to use the Win32 API I wouldn't hold void main against the author.

>
> >> >> Fundamentally, it comes down to this: void main introduces a point
> >> >> of failure which is both completely avoidable and _trivial_ to
> >> >> avoid; why, then, risk failure?
> >> >
> >> > Are you really saying that allowing void main would put such a
> >> > strain on compilers that they could not produce correct code?
> >>
> >> Actually, that may be the case. Or a compiler may choose to not
> >> produce any code at all since the source has an error.
> >>

> >
> > That's a gem of a circular argument. void main is illegal, therefore
> > the compiler needn't compile it, therefore we can't allow void main.

>
> What I actually wanted to say is that void main is illegal, therefore we
> must not use it in our programs. What you seem to say is that "we" use
> void main in our programs, therefore it must be made legal.
>
> > What I am saying (in contrast to Kelsey) is that there is no technical
> > reason that void main could not be part of standard C++. What ever
> > reasons the C++ committee chose to disallow void main (I suspect it
> > was just tradition) were not to do with the infeasibility of having
> > void main as part of C++.

>
> I wonder why it's so important for you to be able to write "void main".
> What exactly doesn't work if main returns int? I don't really see a
> need to allow it.
> Btw, C does allow "void main" as an implementation defined extension in
> the C99 standard.
>


You misunderstand me. I never use void main, nor do I wish to. I'm just
saying I've yet to hear of a logical reason why it is illegal. It just seems
to be an arbitary historical tradition. And since there doesn't seem to be
any logic behind it, its not surprising so many people don't realise what
the rule is.

john




Rolf Magnus 06-29-2003 07:50 PM

Re: Big Deal with returning int
 
John Harrison wrote:

> I think the book argument has some validity when the book is about
> C++. A book about C++ obviously should be written by an author who
> knows the language well. But in a book on algorithms, or graphics or
> networking or how to use the Win32 API I wouldn't hold void main
> against the author.


I was more or less talking about books about C++.

> You misunderstand me. I never use void main, nor do I wish to. I'm
> just saying I've yet to hear of a logical reason why it is illegal. It
> just seems to be an arbitary historical tradition. And since there
> doesn't seem to be any logic behind it, its not surprising so many
> people don't realise what the rule is.


Well, C++ is meant as a portable language, and as I said, on many
systems, every program has an exit code. What would that code be if you
return void? Of course you could make a rule that returning void means
returning 0 under the hood, but I'd say that this is rather a confusing
rule. OTOH, I find it also confusing (and totally inconsistent), that
in C++, you can leave out the return statement in main (and only in
main) even though it has to return an int, and get an implicit return
value of 0 in that case.


Jack Klein 06-29-2003 09:07 PM

Re: Big Deal with returning int
 
On Sun, 29 Jun 2003 13:19:23 +0100, "John Harrison"
<john_andronicus@hotmail.com> wrote in comp.lang.c++:

> >
> > It's quite interesting that you call "void main" common practise, since
> > it actually never was allowed. So you think the rule should be changed,
> > because nobody follows it anyway?

>
> You only have to read here for a while to see that it is common practise.


It's common practice among many of the posters here to include a
header named <conio.h> and call a function named clrscr(). Some of
them are quite indignant that it doesn't work with their new compiler.

[snip]

> What I am saying (in contrast to Kelsey) is that there is no technical
> reason that void main could not be part of standard C++. What ever reasons
> the C++ committee chose to disallow void main (I suspect it was just
> tradition) were not to do with the infeasibility of having void main as part
> of C++.
>
> john


There's no reason that clrscr() is not part of standard C++. Or
CreateWindowEXWithFunnyHungarianParameters(). They could always
return a failure code on platforms where they physically can't exist,
and the program could then always return EXIT_FAILURE from "void
main()".

Actually there's a very good reason for standard C and C++ not to
include "void main()".

A spoken language like English (or French, or Russian, or Hindi, or
any other) grows and evolves through common use. In cases like this,
things that were originally ungrammatical can, do, and should become
part of the language if that's what a large enough percentage of the
speakers use.

Computer languages, on the other hand, do not, and should not have
that flexibility. If they grow and evolve, it should be through
careful experimentation and adoption of useful, tested and verified
existing practice. Not just rewarding the lazy and ignorant by
codifying their laziness and/or ignorance.

What does C++ or C gain by standardizing "void main()"?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

Dill Hole 06-29-2003 09:13 PM

Re: Big Deal with returning int
 
On Sun, 29 Jun 2003 13:59:15 +0200, Rolf Magnus <ramagnus@t-online.de>
wrote:

>I see it a bit different. If an author uses void main, that shows that
>he doesn't know C++ very well. This gives not much trust in the rest of
>the book.
>
>
>It's quite interesting that you call "void main" common practise, since
>it actually never was allowed. So you think the rule should be changed,
>because nobody follows it anyway?


Misspelling "practice" shows that you do not know English very well.
If you do not have English mastered why should we trust your C++
abilities?

Rolf Magnus 06-29-2003 09:21 PM

Re: Big Deal with returning int
 
Dill Hole wrote:

> On Sun, 29 Jun 2003 13:59:15 +0200, Rolf Magnus <ramagnus@t-online.de>
> wrote:
>
>>I see it a bit different. If an author uses void main, that shows that
>>he doesn't know C++ very well. This gives not much trust in the rest
>>of the book.
>>
>>
>>It's quite interesting that you call "void main" common practise,
>>since it actually never was allowed. So you think the rule should be
>>changed, because nobody follows it anyway?

>
> Misspelling "practice" shows that you do not know English very well.
> If you do not have English mastered why should we trust your C++
> abilities?


Mostly because the rules of the English language are not covered by the
C++ standard, so it has nothing to do with each other. If, OTOH, I'd
written a book about English that teaches you wrong spelling, you
should be suspicious, I guess.



All times are GMT. The time now is 04:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.