[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Forgetting to write a return in a function returning a value

hectorchu

11/4/2008 10:40:00 AM

Why doesn't my compiler (g++) flag something like this as an error:

int main()
{
}

?
13 Answers

tpl

11/4/2008 10:49:00 AM

0

hectorchu@gmail.com writes:

>Why doesn't my compiler (g++) flag something like this as an error:

>int main()
>{
>}

Because main's special. Try the same thing in another function.

G Kettleborough

11/4/2008 10:49:00 AM

0

hectorchu@gmail.com wrote:
> Why doesn't my compiler (g++) flag something like this as an error:
>
> int main()
> {
> }
>
> ?

Because it's not an error, in fact is is absolutely correct. In C++ the
void argument is implicit for any function. For main, return 0 is
implicit if you don't write an explicit return. Also for main the only
two proper ways to write it are:

int main()
{
}

and

int main(int argc, char* argv[])
{
}

int main(void) is wrong.

--
George Kettleborough

Jensen Somers

11/4/2008 10:51:00 AM

0

hectorchu@gmail.com wrote:
> Why doesn't my compiler (g++) flag something like this as an error:
>
> int main()
> {
> }
>
> ?

Because in C++ main() is the only function which does not explicitly
needs a return statement [1]. It may also depend on the type of warning
level you specified.

[1] http://www.research.att.com/~bs/bs_faq2.html...

--
Jensen Somers <http://jsom...
Email: -http:// +jensen@

"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe trying to
produce bigger and better idiots. So far, the Universe is winning."
- Rick Cook, The Wizardry Compiled

Victor Bazarov

11/4/2008 1:39:00 PM

0

G Kettleborough wrote:
> hectorchu@gmail.com wrote:
>> Why doesn't my compiler (g++) flag something like this as an error:
>>
>> int main()
>> {
>> }
>>
>> ?
>
> Because it's not an error, in fact is is absolutely correct. In C++
> the void argument is implicit for any function. For main, return 0 is
> implicit if you don't write an explicit return. Also for main the only
> two proper ways to write it are:
>
> int main()
> {
> }
>
> and
>
> int main(int argc, char* argv[])
> {
> }
>
> int main(void) is wrong.

No, it isn't. It's ugly, but it's not wrong.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


hectorchu

11/4/2008 2:34:00 PM

0

On 4 Nov, 10:48, t...@eng.cam.ac.uk (Tim Love) wrote:
> hector...@gmail.com writes:
> >Why doesn't my compiler (g++) flag something like this as an error:
> >int main()
> >{
> >}
>
> Because main's special. Try the same thing in another function.

Following your suggestion, I tried the following:

int f()
{
}

int main()
{
return f();
}

This compiles okay too. So I don't think what I'm describing is
specific to main.

Pawel Dziepak

11/4/2008 2:44:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jensen Somers wrote:
> Because in C++ main() is the only function which does not explicitly
> needs a return statement [1]. It may also depend on the type of warning
> level you specified.

Indeed, it depends. If you had used flag -Wall (in gcc), you would have
got warning for int f().

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail....

iEYEARECAAYFAkkQX5QACgkQPFW+cUiIHNottACeIqONjHogSOIhwP0xxm3SobgI
WPAAni5YA8z2fF6f2aACjBTEREbtQ6bv
=hpct
-----END PGP SIGNATURE-----

Hendrik Schober

11/4/2008 2:52:00 PM

0

hectorchu@gmail.com wrote:
> On 4 Nov, 10:48, t...@eng.cam.ac.uk (Tim Love) wrote:
>> hector...@gmail.com writes:
>>> Why doesn't my compiler (g++) flag something like this as an error:
>>> int main()
>>> {
>>> }
>> Because main's special. Try the same thing in another function.
>
> Following your suggestion, I tried the following:
>
> int f()
> {
> }
>
> int main()
> {
> return f();
> }
>
> This compiles okay too. So I don't think what I'm describing is
> specific to main.

That's compiler-specifc. Comeau complains:
warning: missing return statement at end of non-void function "f"
This
int f() {return 0;}

int main() {}

OTOH, compiles without warning.

Schobi

G Kettleborough

11/4/2008 5:34:00 PM

0

Victor Bazarov wrote:
>
> No, it isn't. It's ugly, but it's not wrong.
>
> V

Well the standard defines only those two. Or do you mean that int
main(void) is simply the explicit version of of int main() so equivalent?
--
George Kettleborough

peter koch

11/4/2008 6:08:00 PM

0

On 4 Nov., 11:39, hector...@gmail.com wrote:
> Why doesn't my compiler (g++) flag something like this as an error:
>
> int main()
> {
>
> }

As others have told you, main is special. It is not required to have
an explicit return, and if it returns via the end, this corresponds to
return 0. Don't ask me why that silly rule was implemented.

Now, take another function:

int foo() {}

Surprisingly this function is also ok: you just can't call it:
execution of f will lead to undefined behaviour. Why is this so?
Because it can be very difficult if not impossible to determine if a
function returns anything or not. Let's expand foo a little:

external void report_error(char const*);
int foo()
{
if (rand() < 10)
return 777;
report_error("Illegal random result in foo");
}

Now, this function might or not return, depending on report_error.
report_error might call exit, it might throw en exception or it might
just log the error and return. It might even do one of the three
things depending on some external state.

It is only when report_error returns, we get undefined behaviour.
Now, you could burden the programmer and require him to write a dummy
return-statement, but this might have its own problems: it adds dead
code to the function, and this can be a burden in some environments,
especially if the stuff returned is something more complicated than a
simple int and it might make code somewhat obfuscated (imagine the
call being replaced by a throw).

That said - if you've followed me so far - every decent compiler will
(or can be made to) warn about the missing return statement, and most
compilers also have the ability to treat certain warnings as errors,
so basically it is a question of setting your environment up in a way
that satisfies your requirements.

/Peter

Ian Collins

11/4/2008 6:19:00 PM

0

hectorchu@gmail.com wrote:
> On 4 Nov, 10:48, t...@eng.cam.ac.uk (Tim Love) wrote:
>> hector...@gmail.com writes:
>>> Why doesn't my compiler (g++) flag something like this as an error:
>>> int main()
>>> {
>>> }
>> Because main's special. Try the same thing in another function.
>
> Following your suggestion, I tried the following:
>
> int f()
> {
> }
>
> int main()
> {
> return f();
> }
>
> This compiles okay too. So I don't think what I'm describing is
> specific to main.

Invoke it in conforming mode and you will.

--
Ian Collins