C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: Big Deal with returning int
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Ian
Guest





PostPosted: Sun Jun 29, 2003 7:29 am    Post subject: Re: Big Deal with returning int Reply with quote



John Harrison wrote:
Quote:
"Min" <nobody (AT) home (DOT) com> wrote in message
news:VgvLa.341602$Vi5.8706632 (AT) news1 (DOT) calgary.shaw.ca...

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.

If nothing is returned, how can what ever called the program tell if it

exited with or without error?

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


Maybe so as not to break legacy code, a bit of a lame excuse.

Ian


Back to top
John Harrison
Guest





PostPosted: Sun Jun 29, 2003 8:50 am    Post subject: Re: Big Deal with returning int Reply with quote




"Klaus Eichner" <klaus_gb (AT) yahoo (DOT) com> wrote

Quote:
"Min" <nobody (AT) home (DOT) com> wrote in message
news:VgvLa.341602$Vi5.8706632 (AT) news1 (DOT) calgary.shaw.ca...
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 ? What difference does it make if "void main" rather than
"int
main" ? It is not that some obscure code that people can't understand
easily. Anyway, would someone explain why ;? not just it is better to
return explicitly.

http://www.research.att.com/~bs/bs_faq2.html#void-main

Well that provides a good explaination of what the situation is and from the
horse's mouth, so to speak. But it still doesn't explain why. I'm still
contending that there is no good explaination.

john



Back to top
Kelsey Bjarnason
Guest





PostPosted: Sun Jun 29, 2003 9:23 am    Post subject: Re: Big Deal with returning int Reply with quote



[snips]

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

Quote:
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



Back to top
Klaus Eichner
Guest





PostPosted: Sun Jun 29, 2003 9:45 am    Post subject: Re: Big Deal with returning int Reply with quote

"Min" <nobody (AT) home (DOT) com> wrote

Quote:
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 ? What difference does it make if "void main" rather than
"int
main" ? It is not that some obscure code that people can't understand
easily. Anyway, would someone explain why ;? not just it is better to
return explicitly.

http://www.research.att.com/~bs/bs_faq2.html#void-main

Quote:
Or How is it possibly cause the problem ?

PS: I am saying this because most of C++ books I found, uses "void main".
If they can't get this fundamental thing right, I guess, I have been
reading
all the garbage from them.



Back to top
John Harrison
Guest





PostPosted: Sun Jun 29, 2003 10:39 am    Post subject: Re: Big Deal with returning int Reply with quote


"Kelsey Bjarnason" <kelseyb (AT) xxnospamyy (DOT) lightspeed.bc.ca> wrote

Quote:
[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.

Quote:

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.

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

Quote:

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.

Quote:

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


john




Back to top
John Harrison
Guest





PostPosted: Sun Jun 29, 2003 10:46 am    Post subject: Re: Big Deal with returning int Reply with quote

Quote:

http://www.research.att.com/~bs/bs_faq2.html#void-main

Well that provides a good explaination of what the situation is and from
the
horse's mouth, so to speak. But it still doesn't explain why. I'm still
contending that there is no good explaination.

I don't think there is a conclusive explanation in the standard,

I wouldn't expect it to be there.

Quote:
neither do
I believe that such an explanation is necessary.

Not necessary perhaps, but it would be nice to know.

Quote:
There is, however, a lot of
discussion out there, especially in comp.std.c++. See, for example, the
discussion started by Huw Ford, Subject: Why int main()?Newsgroups:
comp.std.c++, date: 2003-04-11 02:52:13 PST

Thanks, I'll take a look.

john



Back to top
John Harrison
Guest





PostPosted: Sun Jun 29, 2003 11:03 am    Post subject: Re: Big Deal with returning int Reply with quote

Quote:

There is, however, a lot of
discussion out there, especially in comp.std.c++. See, for example, the
discussion started by Huw Ford, Subject: Why int main()?Newsgroups:
comp.std.c++, date: 2003-04-11 02:52:13 PST

Thanks, I'll take a look.

john

Seems to me the reason is probably to do with the historical development of
C and C++ (in particular the later introduction of void to C and the banning
of implicit int return in C++). So no good objective reason just historical
accident, which is fair enough.

john



Back to top
Klaus Eichner
Guest





PostPosted: Sun Jun 29, 2003 11:42 am    Post subject: Re: Big Deal with returning int Reply with quote

"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote

Quote:

"Klaus Eichner" <klaus_gb (AT) yahoo (DOT) com> wrote in message
news:3efea6fa$0$12457$626a54ce (AT) news (DOT) free.fr...
"Min" <nobody (AT) home (DOT) com> wrote in message
news:VgvLa.341602$Vi5.8706632 (AT) news1 (DOT) calgary.shaw.ca...
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 ? What difference does it make if "void main" rather
than
"int
main" ? It is not that some obscure code that people can't understand
easily. Anyway, would someone explain why ;? not just it is better to
return explicitly.

http://www.research.att.com/~bs/bs_faq2.html#void-main

Well that provides a good explaination of what the situation is and from
the
horse's mouth, so to speak. But it still doesn't explain why. I'm still
contending that there is no good explaination.

I don't think there is a conclusive explanation in the standard, neither do
I believe that such an explanation is necessary. There is, however, a lot of
discussion out there, especially in comp.std.c++. See, for example, the
discussion started by Huw Ford, Subject: Why int main()?Newsgroups:
comp.std.c++, date: 2003-04-11 02:52:13 PST
http://tinyurl.com/fjww

Quote:
john





Back to top
Rolf Magnus
Guest





PostPosted: Sun Jun 29, 2003 11:47 am    Post subject: Re: Big Deal with returning int Reply with quote

Min wrote:

Quote:
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 ? What difference does it make if "void
main" rather than "int main" ?

void main() is just plain wrong. Some compilers support it as an
(actually unneccesary) extension to the language, so on those specific
compilers, it might work, but that's not guaranteed. The stanard C++
language (as C) _never_ permited any other return type than int. This
rule is older than some programmers, and still there are people who get
it wrong. Btw, if everyone keeps telling you that void main() is wrong,
what's the big deal about changing it to int and just using that?

Quote:
It is not that some obscure code that people can't understand
easily. Anyway, would someone explain why ;? not just it is better to
return explicitly.

Or How is it possibly cause the problem ?

A compiler may not accept it, and it is supposed to issue at least a
warning if you try to return void from main. Also, on many systems, the
exit code (i.e. the value returned from main) is important for the
parent process. If you return void, the parent may get garbage instead
of a reasonable return code. A system also is (from the C++ standard's
POV) allowed to crash completely if no sensible value is returned. I
don't actually know a system that does that, but this means nothing.

Quote:
PS: I am saying this because most of C++ books I found, uses "void
main". If they can't get this fundamental thing right, I guess, I have
been reading all the garbage from them.

I see it the same way. If some C++ book or tutorial has examples that
contain "void main", I'd throw it away. As you say, if they don't get
such a fundamental and simple thing right (or just don't know that it's
wrong), then they probably don't know the C++ language very well, and
other more severe flaws are likely to be lurking there.



Back to top
Rolf Magnus
Guest





PostPosted: Sun Jun 29, 2003 11:59 am    Post subject: Re: Big Deal with returning int Reply with quote

John Harrison wrote:

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

Quote:
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?

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

Quote:
I don't think
so.


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


john


Back to top
John Harrison
Guest





PostPosted: Sun Jun 29, 2003 12:19 pm    Post subject: Re: Big Deal with returning int Reply with quote

Quote:

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.

Quote:

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



Back to top
Rolf Magnus
Guest





PostPosted: Sun Jun 29, 2003 1:43 pm    Post subject: Re: Big Deal with returning int Reply with quote

John Harrison wrote:

Quote:

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

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

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


Back to top
John Harrison
Guest





PostPosted: Sun Jun 29, 2003 2:00 pm    Post subject: Re: Big Deal with returning int Reply with quote


"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote

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

Quote:

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




Back to top
Samuele Armondi
Guest





PostPosted: Sun Jun 29, 2003 4:21 pm    Post subject: Re: Big Deal with returning int Reply with quote

"Julián Albo" <JULIANALBO (AT) terra (DOT) es> wrote

Samuele Armondi escribió:

Quote:
systems. Obviously, most compilers will be smart enough to substitute int
main() for void main() and add a return 0; to the end of it, but it
defeats
the purpose of returning a value at all, does it not?

The compilers i use in ms-dos (time ago) does not add a return 0. void
main is called just like int main, and the value returned is whatever is
in the ax register.

Regards.

Even more reason to use int main(). "whatever is in the ax register" does
not give the OS any information as to whether or not the code was
successful.
S. Armondi




Back to top
Jonathan Clements
Guest





PostPosted: Sun Jun 29, 2003 6:00 pm    Post subject: Re: Big Deal with returning int Reply with quote

So, what's the difference between

int main()
{
/* whatever */
return(0); // we've done okay
}

and

void main()
{
/* whatever */
exit(EXIT_SUCCESS); // we've done okay
}

I'm probably missing something major here, if so, please forgive me.

Jon.

"Samuele Armondi" <sammyboyuk_NOSPAM_ (AT) hotmail (DOT) com> wrote

Quote:

"Min" <nobody (AT) home (DOT) com> wrote in message
news:VgvLa.341602$Vi5.8706632 (AT) news1 (DOT) calgary.shaw.ca...
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 ? What difference does it make if "void main" rather than
"int
main" ? It is not that some obscure code that people can't understand
easily. Anyway, would someone explain why ;? not just it is better to
return explicitly.

Or How is it possibly cause the problem ?

PS: I am saying this because most of C++ books I found, uses "void
main".
If they can't get this fundamental thing right, I guess, I have been
reading
all the garbage from them.


main () needs to return an int to the operating system to let it know if
it
was successful or not. Otherwise why did people use exit(-1) to indicate
an
error? And taking MS-DOS as an example, have you ever seen things like "if
errorlevel 1" in batch files? If main does not return a value indicating
whether it was successful or not, how can the operating system correctly
set
the errorlevel variable? The same argument applies to other operating
systems. Obviously, most compilers will be smart enough to substitute int
main() for void main() and add a return 0; to the end of it, but it
defeats
the purpose of returning a value at all, does it not?
S. Armondi





Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.