Forgetting to write a return in a function returning a value

Discussion in 'C++' started by hectorchu@gmail.com, Nov 4, 2008.

  1. Guest

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

    int main()
    {
    }

    ?
     
    , Nov 4, 2008
    #1
    1. Advertising

  2. Tim Love Guest

    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.
     
    Tim Love, Nov 4, 2008
    #2
    1. Advertising

  3. 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
     
    G Kettleborough, Nov 4, 2008
    #3
  4. Guest

    On 4 Nov, 10:48, (Tim Love) wrote:
    > 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.
     
    , Nov 4, 2008
    #4
  5. 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
     
    George Kettleborough, Nov 4, 2008
    #5
  6. peter koch Guest

    On 4 Nov., 11:39, 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
     
    peter koch, Nov 4, 2008
    #6
  7. Ian Collins Guest

    wrote:
    > On 4 Nov, 10:48, (Tim Love) wrote:
    >> 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
     
    Ian Collins, Nov 4, 2008
    #7
  8. wrote:
    > Why doesn't my compiler (g++) flag something like this as an error:
    >
    > int main()
    > {
    > }
    >
    > ?


    Because in C++ it is not an error, in a sense that it is not ill-formed.
    (And no, in that regard 'main' is no different from any other
    function). C++ language specification does not require compilers to
    detect and diagnose execution paths that flow off the end of a
    value-returning function without an explicit 'return' statement. In
    general case, flowing off in such a context produces undefined behavior
    (not in 'main' though, which is where is does indeed get special), but
    the program still remains well-formed. In other words, it is your
    responsibility to remember to include an explicit 'return' statement
    into every possible control path of a value-returning function. Some
    compilers might help you to detect the paths where you forgot to provide
    a 'return', but in any case they are not required to do so.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 4, 2008
    #8
  9. James Kanze Guest

    On Nov 4, 7:18 pm, Ian Collins <> wrote:
    > wrote:
    > > On 4 Nov, 10:48, (Tim Love) wrote:
    > >> 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.


    Not necessarily; it's undefined behavior. And compilers that
    warn when in fact you can't reach the end can be very irritating
    as well.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 5, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Sherman

    Forgetting how to use vec

    Aaron Sherman, Feb 5, 2004, in forum: Perl
    Replies:
    0
    Views:
    600
    Aaron Sherman
    Feb 5, 2004
  2. Snoeys Andy
    Replies:
    2
    Views:
    415
    White Wolf
    Sep 12, 2003
  3. Siemel Naran
    Replies:
    1
    Views:
    281
    Phlip
    Dec 8, 2004
  4. Philip Smith
    Replies:
    7
    Views:
    346
  5. 010 010

    why is fread forgetting where its position?

    010 010, Sep 29, 2006, in forum: C Programming
    Replies:
    13
    Views:
    811
    010 010
    Oct 3, 2006
Loading...

Share This Page