gcc bug ?

Discussion in 'C Programming' started by -de-, May 20, 2008.

  1. -de-

    -de- Guest

    Dear experts !


    Consider the following code snippet :

    /*================================*/
    /* void.c */
    void f(int i)
    {
    i++;
    if (i!=10)
    return f(i);
    }

    int main(void)
    {
    f(0);
    return 0;
    }
    /*================================*/


    gcc warns me with the following message:


    -------------------------------------------
    $ gcc -g -W -Wall -pedantic -o x void.c
    void.c: In function "f":
    void.c:5: attention : "return" with a value, in function returning void
    -------------------------------------------


    "return with a value" ? what a nonsense !

    The function f returning no value (void), the instruction
    return f();
    isn't an error. It's equivalent to a return statement, apart from the
    function call.

    This is a compiler's bug, isn't it ?

    Thanks.
    -de-, May 20, 2008
    #1
    1. Advertising

  2. On Tue, 20 May 2008 22:55:37 +0200, -de- wrote:
    > void f(int i)
    > {

    [...]
    > return f(i);

    [...]
    > void.c:5: attention : "return" with a value, in function returning void


    I'm assuming this is a translated error message?

    > This is a compiler's bug, isn't it ?


    No, it isn't. Firstly, it's not a fatal error, and compilers are allowed
    to give messages for any code, no matter how correct, and commonly do so
    for valid but questionable code. Secondly, what you're doing is not just
    poor style but is actually disallowed in C, and some other compilers will
    give a hard error for it. If you want to call f(i) and then return, write
    that.

    void f(int i)
    {
    [...]
    f(i);
    return;
    [...]
    }
    Harald van Dijk, May 20, 2008
    #2
    1. Advertising

  3. -de- <> writes:
    > Consider the following code snippet :
    >
    > /*================================*/
    > /* void.c */
    > void f(int i)
    > {
    > i++;
    > if (i!=10)
    > return f(i);
    > }
    >
    > int main(void)
    > {
    > f(0);
    > return 0;
    > }
    > /*================================*/
    >
    >
    > gcc warns me with the following message:
    >
    >
    > -------------------------------------------
    > $ gcc -g -W -Wall -pedantic -o x void.c
    > void.c: In function "f":
    > void.c:5: attention : "return" with a value, in function returning void
    > -------------------------------------------
    >
    >
    > "return with a value" ? what a nonsense !

    [...]

    Probably the error message should say
    "return" with an expression

    C99 6.8.6.4:

    Constraints

    A return statement with an expression shall not appear in a
    function whose return type is void. A return statement without an
    expression shall only appear in a function whose return type is
    void.

    It follows from this that the expression, if any, in a return
    statement cannot be of type void.

    Your code would make sense if the language permitted it (you're
    returning a void expression from a void function), but it doesn't, so
    you don't get to do that.

    Since you're not actually returning a value, you can replace this:

    if (i != 10)
    return f(i);

    with this:

    if (i != 10) {
    f(i);
    return;
    }

    or, in this particular case, with just this:

    if (i != 10)
    f(i);

    since you're about to fall off the end of the function anyway, so the
    return statement is superfluous.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 20, 2008
    #3
  4. On Tue, 20 May 2008 23:11:33 +0200, Pietro Cerutti wrote:
    > Harald van Dijk wrote:
    > [snip]
    >> Secondly, what you're doing is not just poor style but is actually
    >> disallowed in C

    > [more snip]
    >
    > Interesting, would you like to elaborate it?


    6.8.6.4 The return statement
    Constraints
    1 A return statement with an expression shall not appear in a function
    whose return type is void. A return statement without an expression shall
    only appear in a function whose return type is void.

    This disallows

    void f(void);
    void g(void) {
    return f();
    }

    because while f() can be argued not to have a value, it is still an
    expression.
    Harald van Dijk, May 20, 2008
    #4
  5. -de- <> writes:

    > Consider the following code snippet :
    >
    > /*================================*/
    > /* void.c */
    > void f(int i)
    > {
    > i++;
    > if (i!=10)
    > return f(i);
    > }
    >
    > int main(void)
    > {
    > f(0);
    > return 0;
    > }
    > /*================================*/
    >
    > gcc warns me with the following message:
    >
    > void.c: In function "f":
    > void.c:5: attention : "return" with a value, in function returning void
    >
    > "return with a value" ? what a nonsense !


    The wording is not ideal, but it is not nonsense!

    > The function f returning no value (void), the instruction
    > return f();
    > isn't an error. It's equivalent to a return statement, apart from the
    > function call.
    >
    > This is a compiler's bug, isn't it ?


    No. A diagnostic is required since the program violates a
    constraint:

    6.8.6.4 The return statement

    Constraints

    1 A return statement with an expression shall not appear in a function
    whose return type is void. A return statement without an expression
    shall only appear in a function whose return type is void.

    A better message might have been "return with an expression in a
    function whose return type is void", but it is the construct that is
    wrong, not the compiler.

    The "obvious" way to write what you want is just:

    void f(int i)
    {
    i++;
    if (i != 10)
    f(i);
    }

    --
    Ben.
    Ben Bacarisse, May 20, 2008
    #5
  6. -de-

    -de- Guest

    Harald van Dijk a écrit :

    >> void.c:5: attention : "return" with a value, in function returning void

    >
    > I'm assuming this is a translated error message?



    maybe but the translation is not mine ;)
    -de-, May 20, 2008
    #6
  7. On 20 May, 22:17, Keith Thompson <> wrote:
    > -de- <> writes:
    > > Consider the following code snippet :

    >
    > > /*================================*/
    > > /* void.c */
    > > void f(int i)
    > > {
    > > i++;
    > > if (i!=10)
    > > return f(i);
    > > }

    >
    > > int main(void)
    > > {
    > > f(0);
    > > return 0;
    > > }
    > > /*================================*/

    >
    > > gcc warns me with the following message:

    >
    > > -------------------------------------------
    > > $ gcc -g -W -Wall -pedantic -o x void.c
    > > void.c: In function "f":
    > > void.c:5: attention : "return" with a value, in function returning void
    > > -------------------------------------------

    >
    > > "return with a value" ? what a nonsense !

    >
    > [...]
    >
    > Probably the error message should say
    > "return" with an expression
    >
    > C99 6.8.6.4:
    >
    > Constraints
    >
    > A return statement with an expression shall not appear in a
    > function whose return type is void. A return statement without an
    > expression shall only appear in a function whose return type is
    > void.
    >
    > It follows from this that the expression, if any, in a return
    > statement cannot be of type void.
    >
    > Your code would make sense if the language permitted it (you're
    > returning a void expression from a void function), but it doesn't, so
    > you don't get to do that.


    What do "expression of type void" and "void expression"
    mean ?
    Spiros Bousbouras, May 20, 2008
    #7
  8. On May 20, 5:23 pm, Pietro Cerutti <gahr_SPAM_gahr_ME_ch> wrote:
    > Keith Thompson wrote:
    > > -de- <> writes:
    > >> Consider the following code snippet :

    >
    > >> /*================================*/
    > >> /* void.c */
    > >> void f(int i)
    > >> {
    > >> i++;
    > >> if (i!=10)
    > >> return f(i);
    > >> }

    >
    > >> int main(void)
    > >> {
    > >> f(0);
    > >> return 0;
    > >> }
    > >> /*================================*/

    >
    > >> gcc warns me with the following message:

    >
    > >> -------------------------------------------
    > >> $ gcc -g -W -Wall -pedantic -o x void.c
    > >> void.c: In function "f":
    > >> void.c:5: attention : "return" with a value, in function returning void
    > >> -------------------------------------------

    >
    > >> "return with a value" ? what a nonsense !

    > > [...]

    >
    > > Probably the error message should say
    > > "return" with an expression

    >
    > > C99 6.8.6.4:

    >
    > > Constraints

    >
    > > A return statement with an expression shall not appear in a
    > > function whose return type is void. A return statement without an
    > > expression shall only appear in a function whose return type is
    > > void.

    >
    > I was also referring to this paragraph when giving my answer.
    >
    > What wasn't clear to me is "is a void expression an expression at all"?
    >
    > 6.5.1
    > "An expression is a sequence of operators and operands that specifies
    > computation of a value"
    >
    > and
    >
    > 6.3.2.2.1
    > "(A void expression is evaluated for its side effects.)"
    >
    > So, it seems to me that a void expression doesn't "specify computation
    > of a value", thus is not an expression.
    >
    > This leads me to say that
    >
    > void somefunc(void)
    > {
    > int i = 0;
    > return (void)i;
    >
    > }
    >
    > is correct.
    >
    > Please correct me..


    Um, let's see:

    1) It's called a void *expression*.

    2) The first part of section 6.3.2.2p1 (which you quoted the end of)
    states:

    "The (nonexistent) value of a void expression (an expression that has
    type void)"
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^

    3) The entire part of section 6.5p1 (which you quoted the beginning
    of) states:

    "An expression is a sequence of operators and operands that specifies
    computation of a
    value, or that designates an object or a function, or that generates
    side effects, or that
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    performs a combination thereof."

    --
    Robert Gamble
    Robert Gamble, May 20, 2008
    #8
  9. Pietro Cerutti <gahr_SPAM_gahr_ME_ch> writes:
    > Keith Thompson wrote:
    >> -de- <> writes:
    >>> Consider the following code snippet :
    >>>
    >>> /*================================*/
    >>> /* void.c */
    >>> void f(int i)
    >>> {
    >>> i++;
    >>> if (i!=10)
    >>> return f(i);
    >>> }
    >>>
    >>> int main(void)
    >>> {
    >>> f(0);
    >>> return 0;
    >>> }
    >>> /*================================*/
    >>>
    >>>
    >>> gcc warns me with the following message:
    >>>
    >>>
    >>> -------------------------------------------
    >>> $ gcc -g -W -Wall -pedantic -o x void.c
    >>> void.c: In function "f":
    >>> void.c:5: attention : "return" with a value, in function returning void
    >>> -------------------------------------------
    >>>
    >>>
    >>> "return with a value" ? what a nonsense !

    >> [...]
    >> Probably the error message should say
    >> "return" with an expression
    >> C99 6.8.6.4:
    >> Constraints
    >> A return statement with an expression shall not appear in a
    >> function whose return type is void. A return statement without an
    >> expression shall only appear in a function whose return type is
    >> void.

    >
    > I was also referring to this paragraph when giving my answer.
    >
    > What wasn't clear to me is "is a void expression an expression at all"?
    >
    > 6.5.1
    > "An expression is a sequence of operators and operands that specifies
    > computation of a value"
    >
    > and
    >
    > 6.3.2.2.1
    > "(A void expression is evaluated for its side effects.)"
    >
    > So, it seems to me that a void expression doesn't "specify computation
    > of a value", thus is not an expression.
    >
    > This leads me to say that
    >
    > void somefunc(void)
    > {
    > int i = 0;
    > return (void)i;
    > }
    >
    > is correct.
    >
    > Please correct me..


    Ok.

    The standard's definition of "expression" is not very good. (See also
    previous discussions regarding the standard's definition of "lvalue".)

    Taking the definition literally, an expression must include two or
    more operators (since the plural is used) *and* two or more operands.
    This:

    42

    has *no* operators and *no* operands (it might be argued that 42 is an
    operand, but that doesn't affect the issue), but it's clearly an
    expression.

    If you look at the grammar for "expression", scattered through C99
    section 6.5, it's clear that both
    42
    and
    (void)i
    are expressions.

    And if ``(void)i'' weren't an expression, then the grammar for a
    return statement:

    return expression(opt) ;

    would imply that ``return (void)i;'' is illegal anyway. (It would
    also make most C programs illegal.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 20, 2008
    #9
  10. Spiros Bousbouras <> writes:
    > On 20 May, 22:17, Keith Thompson <> wrote:

    [...]
    >> Your code would make sense if the language permitted it (you're
    >> returning a void expression from a void function), but it doesn't, so
    >> you don't get to do that.

    >
    > What do "expression of type void" and "void expression"
    > mean ?


    A void expression is an expression of type void.

    An expression of type void is, well, an expression whose type is void.
    Examples are a call to a function that returns void, and any
    expression cast to type void.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 20, 2008
    #10
  11. On Tue, 20 May 2008 23:27:18 +0200, Pietro Cerutti wrote:
    > Harald van Dijk wrote:
    >> 6.8.6.4 The return statement
    >> Constraints
    >> 1 A return statement with an expression shall not appear in a function
    >> whose return type is void. A return statement without an expression
    >> shall only appear in a function whose return type is void.
    >>
    >> This disallows
    >>
    >> void f(void);
    >> void g(void) {
    >> return f();
    >> }
    >>
    >> because while f() can be argued not to have a value, it is still an
    >> expression.

    >
    > Would you like to see my answer to Keith and try to elaborate that? ;)


    You already have your answer, but here's another approach:

    The syntax of the return statement is:

    jump-statement:
    [...]
    return expression[opt] ;

    This means there are two forms of the return statement. They are:

    return ;
    return expression ;

    If f() is an expression, return f(); violates 6.8.6.4p1. If f() is not an
    expression, return f(); is a syntax error. So it doesn't really matter,
    either way, the compiler must complain.
    Harald van Dijk, May 20, 2008
    #11
    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. Replies:
    8
    Views:
    428
  2. Kevin P. Fleming

    C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1

    Kevin P. Fleming, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    644
    Kevin P. Fleming
    Nov 6, 2003
  3. Replies:
    5
    Views:
    357
    Nathan Addy
    Sep 17, 2005
  4. ashnin

    GCC 3.4.3 and GCC 4.1.2

    ashnin, Jul 7, 2008, in forum: C++
    Replies:
    1
    Views:
    518
    Michael DOUBEZ
    Jul 7, 2008
  5. kas
    Replies:
    1
    Views:
    315
    red floyd
    Apr 22, 2010
Loading...

Share This Page