Re: Big Deal with returning int

Discussion in 'C++' started by Jonathan Clements, Jun 29, 2003.

  1. 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" <> wrote in message
    news:3eff03b9$...
    >
    > "Min" <> wrote in message
    > news:VgvLa.341602$...
    > > 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
    >
    >
    Jonathan Clements, Jun 29, 2003
    #1
    1. Advertising

  2. Not at all Ralf...

    I'm just asking what the difference is between exit(EXIT_SUCCESS); and
    return(0);

    Yours Sincerely,

    Jon.

    "Rolf Magnus" <> wrote in message
    news:bdngch$4jn$00$-online.com...
    > Jonathan Clements wrote:
    >
    > > 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.

    >
    > So you suggest that C++ adds a rule that you _must_ call exit() from
    > main? If not, what would happen if you don't?
    >
    Jonathan Clements, Jun 29, 2003
    #2
    1. Advertising

  3. Jonathan Clements

    Rolf Magnus Guest

    Don't top-post. Rearranged.

    Jonathan Clements wrote:

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

    >>
    >> So you suggest that C++ adds a rule that you _must_ call exit() from
    >> main? If not, what would happen if you don't?
    >>

    > Not at all Ralf...
    >
    > I'm just asking what the difference is between exit(EXIT_SUCCESS); and
    > return(0);


    Ah, ok then. AFAIK, there is none.
    Rolf Magnus, Jun 29, 2003
    #3
  4. Jonathan Clements

    Jerry Coffin Guest

    In article <bdn983$q63$1$>,
    says...
    > 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.


    The difference is fairly simple. The problem with defining main to
    return void is that the environment has to set up a proper stack frame
    to call main. As it happens, on quite a few machines, the stack frame
    for a function that returns an int (or most other integer types) will be
    the same as for one that returns void. On these machines, the error in
    the code will be masked.

    OTOH, there's no guarantee that all machines use the same type of stack
    frame for different return types. Just for example, with larger return
    types (e.g. when you return a large struct) it's fairly common for the
    compiler to create a spot on the stack for the return value, and
    basically pass it as a hidden parameter to the function. If a compiler
    happened to do this with functions that return int, we'd get a stack
    misalignment -- the RTL that's calling main builds a stack frame that
    consists of a return value at the top of the stack, and a return address
    right below that. You, however, have defined main to return void, so
    the compiler produces code that expects NO return value, with the return
    address right at the top of the stack. In this case, attempting to
    return from main will NOT return to the right address at all -- instead,
    it'll treat the (uninitialized) return VALUE spot as if it was the
    return address, and jump to wherever it happens to point. In that case,
    about the best you can hope is that your program gets shut down quickly,
    before it has a chance to do any real damage.

    Now, it might initially seem like calling exit instead of returning from
    main would avoid this, but there's no guarantee of that either -- quite
    a few compilers support what are typically called intrinsic functions,
    meaning the compiler generates in-line code for the function under at
    least some circumstances. It might easily detect calls to exit from
    main, and convert them into returns from main, getting us right back to
    the same problem.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jun 30, 2003
    #4
  5. Jonathan Clements

    Andre Kostur Guest

    Rolf Magnus <> wrote in news:bdnkvo$f0$05$-
    online.com:

    > Don't top-post. Rearranged.
    >
    > Jonathan Clements wrote:
    >
    >>> > 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.
    >>>
    >>> So you suggest that C++ adds a rule that you _must_ call exit() from
    >>> main? If not, what would happen if you don't?
    >>>

    >> Not at all Ralf...
    >>
    >> I'm just asking what the difference is between exit(EXIT_SUCCESS); and
    >> return(0);

    >
    > Ah, ok then. AFAIK, there is none.


    Umm.. is that defined by the Standard? Or have I managed to hit a QoI
    issue that when I call exit(), destructors aren't called?
    Andre Kostur, Jun 30, 2003
    #5
  6. Rolf Magnus wrote:
    [...]
    > So you suggest that C++ adds a rule that you _must_ call exit() from
    > main? If not, what would happen if you don't?


    Initial thread would terminate.

    regards,
    alexander.
    Alexander Terekhov, Jun 30, 2003
    #6
  7. Rolf, my apologies, just realised I typed your name as Ralf.

    Sincerely yours,

    Jon.

    "Rolf Magnus" <> wrote in message
    news:bdnkvo$f0$05$-online.com...
    > Don't top-post. Rearranged.
    >
    > Jonathan Clements wrote:
    >
    > >> > 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.
    > >>
    > >> So you suggest that C++ adds a rule that you _must_ call exit() from
    > >> main? If not, what would happen if you don't?
    > >>

    > > Not at all Ralf...
    > >
    > > I'm just asking what the difference is between exit(EXIT_SUCCESS); and
    > > return(0);

    >
    > Ah, ok then. AFAIK, there is none.
    >
    Jonathan Clements, Jun 30, 2003
    #7
  8. Jonathan Clements

    Rolf Magnus Guest

    Alexander Terekhov wrote:

    >
    > Rolf Magnus wrote:
    > [...]
    >> So you suggest that C++ adds a rule that you _must_ call exit() from
    >> main? If not, what would happen if you don't?

    >
    > Initial thread would terminate.


    With which return code?
    Rolf Magnus, Jul 1, 2003
    #8
    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. Ian
    Replies:
    0
    Views:
    644
  2. John Harrison

    Re: Big Deal with returning int

    John Harrison, Jun 29, 2003, in forum: C++
    Replies:
    5
    Views:
    817
    Karl Heinz Buchegger
    Jun 30, 2003
  3. Kelsey Bjarnason

    Re: Big Deal with returning int

    Kelsey Bjarnason, Jun 29, 2003, in forum: C++
    Replies:
    38
    Views:
    1,619
    Andre Kostur
    Jul 1, 2003
  4. Rolf Magnus

    Re: Big Deal with returning int

    Rolf Magnus, Jun 29, 2003, in forum: C++
    Replies:
    0
    Views:
    627
    Rolf Magnus
    Jun 29, 2003
  5. Samuele Armondi

    Re: Big Deal with returning int

    Samuele Armondi, Jun 29, 2003, in forum: C++
    Replies:
    0
    Views:
    688
    Samuele Armondi
    Jun 29, 2003
Loading...

Share This Page