missing return statement on non void functions; just a warning?

Discussion in 'C++' started by macracan, Feb 8, 2008.

  1. macracan

    macracan Guest

    I'm wondering what was the rationale that caused gcc to only have a
    warning (disabled by default!!) to signal the following:

    int a()
    {}; // intentionally no statements

    int main()
    {
    printf("%d", a());
    return 0;
    }

    Function a() is declared as returning an int and the compiler doesn't
    treat as an error the missing return statement.
    I'm asking in this forum because I suspect that gcc crowd will just
    tell me to read the standard. If I'm in the wrong forum, I hope to be
    corrected.

    Thank you,
    Adrian
    macracan, Feb 8, 2008
    #1
    1. Advertising

  2. macracan

    Ian Collins Guest

    macracan wrote:
    > I'm wondering what was the rationale that caused gcc to only have a
    > warning (disabled by default!!) to signal the following:
    >
    > int a()
    > {}; // intentionally no statements
    >
    > int main()
    > {
    > printf("%d", a());
    > return 0;
    > }
    >
    > Function a() is declared as returning an int and the compiler doesn't
    > treat as an error the missing return statement.
    > I'm asking in this forum because I suspect that gcc crowd will just
    > tell me to read the standard. If I'm in the wrong forum, I hope to be
    > corrected.
    >

    You must have compiled it as C, it certainly won't compile as C++.

    --
    Ian Collins.
    Ian Collins, Feb 8, 2008
    #2
    1. Advertising

  3. * macracan:
    > I'm wondering what was the rationale that caused gcc to only have a
    > warning (disabled by default!!) to signal the following:
    >
    > int a()
    > {}; // intentionally no statements
    >
    > int main()
    > {
    > printf("%d", a());
    > return 0;
    > }
    >
    > Function a() is declared as returning an int and the compiler doesn't
    > treat as an error the missing return statement.
    > I'm asking in this forum because I suspect that gcc crowd will just
    > tell me to read the standard. If I'm in the wrong forum, I hope to be
    > corrected.


    You're in the group where people will tell you to read the standard... ;-)

    When adding the missing #include, and removing erronous semicolon:

    <quote>
    V:\> for %i in (gnuc.bat) do @type "%~$PATH:i"
    @g++ -O -pedantic -std=c++98 -Wall -finput-charset=windows-1252 %*

    V:\> gnuc --version | find "g++"
    g++ (GCC) 3.4.4 (mingw special)

    V:\> gnuc vc_project.cpp
    vc_project.cpp: In function `int a()':
    vc_project.cpp:4: warning: control reaches end of non-void function

    V:\>_
    </quote>

    Note the elegance and sheer genius-like simplicity of Windows' default
    command interpreter! :)

    But anyway, as a general rule, up the warning level to max, with any
    compiler. With g++ note that optimization is necessary for some
    warnings (I think e.g. use of uninitialized). Otherwise the machinery
    that detects those bad things won't be involved and so no warnings.

    It might also be necessary to turn on specific warnings.


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Feb 8, 2008
    #3
  4. macracan

    Jack Klein Guest

    On Thu, 7 Feb 2008 18:36:49 -0800 (PST), macracan <>
    wrote in comp.lang.c++:

    > I'm wondering what was the rationale that caused gcc to only have a
    > warning (disabled by default!!) to signal the following:


    Despite what you say below, the proper place to ask is someplace where
    the gcc maintainers are active.


    > int a()
    > {}; // intentionally no statements
    >
    > int main()
    > {
    > printf("%d", a());
    > return 0;
    > }
    >
    > Function a() is declared as returning an int and the compiler doesn't
    > treat as an error the missing return statement.


    As someone else already said, you would probably get different
    behavior if you compiled this as C++ as well as C. This is not a
    language violation in C, it merely generates undefined behavior if any
    caller of the function attempts to use the non-existant return value.

    > I'm asking in this forum because I suspect that gcc crowd will just
    > tell me to read the standard. If I'm in the wrong forum, I hope to be
    > corrected.


    It is not a language issue. Neither the C++ nor C standards define
    anything like "warning" or "error". Certain syntax errors, constraint
    violations, and other violations of the language rules require that
    the compiler produce a diagnostic. In addition, a compiler is allowed
    to issue any additional diagnostics its developers see fit.

    The form and content of the diagnostic, and whether an implementation
    chooses to label some diagnostics "error" and others as "warning", is
    entirely a QOI (Quality Of Implementation) issue.

    The fact that your compiler issued any message at all meets the
    diagnostic requirement in C++, and completely exceeds it in C.

    If you don't agree with the classification of "warning" versus
    "error", that is entirely a matter for the compiler implementers.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 8, 2008
    #4
    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. Ollej Reemt
    Replies:
    7
    Views:
    499
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    775
    The Real OS/2 Guy
    Oct 28, 2003
  3. David M. Wilson
    Replies:
    8
    Views:
    478
    Ben Pfaff
    Jan 7, 2004
  4. Replies:
    5
    Views:
    815
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    392
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page