Accelerated C++ - exercise 0-1

Discussion in 'C++' started by arnuld, Mar 18, 2007.

  1. arnuld

    arnuld Guest

    Q: what does this statement do ?

    3 + 4;


    i am not able to answer it, except that it is an error BUT this is
    what i got from Terminal:

    -------------- PROGRAME --------------
    int main()
    {
    3 + 4;

    return 0;
    }
    -------------- OUTPUT -----------------
    [arch@voodo acc-cpp]$ g++ -ansi -pedantic -Wall -Wextra new.cpp
    new.cpp: In function 'int main()':
    new.cpp:3: warning: statement has no effect
    [arch@voodo acc-cpp]$
    arnuld, Mar 18, 2007
    #1
    1. Advertising

  2. arnuld

    Du~ Guest

    On Mar 17, 9:29 pm, "arnuld" <> wrote:
    > Q: what does this statement do ?
    >
    > 3 + 4;
    >
    > i am not able to answer it, except that it is an error BUT this is
    > what i got from Terminal:
    >
    > -------------- PROGRAME --------------
    > int main()
    > {
    > 3 + 4;
    >
    > return 0;}
    >
    > -------------- OUTPUT -----------------
    > [arch@voodo acc-cpp]$ g++ -ansi -pedantic -Wall -Wextra new.cpp
    > new.cpp: In function 'int main()':
    > new.cpp:3: warning: statement has no effect
    > [arch@voodo acc-cpp]$


    check where it is stored.
    Du~, Mar 18, 2007
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    > On Mar 18, 10:33 am, "Du~" <> wrote:
    > On Mar 17, 9:29 pm, "arnuld" <> wrote:


    > check where it is stored.


    what that means ?

    your reply is beyond my understanding

    :-(
    arnuld, Mar 18, 2007
    #3
  4. arnuld

    Jim Langston Guest

    "arnuld" <> wrote in message
    news:...
    > Q: what does this statement do ?
    >
    > 3 + 4;
    >
    >
    > i am not able to answer it, except that it is an error BUT this is
    > what i got from Terminal:
    >
    > -------------- PROGRAME --------------
    > int main()
    > {
    > 3 + 4;
    >
    > return 0;
    > }
    > -------------- OUTPUT -----------------
    > [arch@voodo acc-cpp]$ g++ -ansi -pedantic -Wall -Wextra new.cpp
    > new.cpp: In function 'int main()':
    > new.cpp:3: warning: statement has no effect
    > [arch@voodo acc-cpp]$


    It is not an error, it is a warning. And the warning tells you what it
    does. It has no effect. Basically nothing. The compiler may even optimize
    it away to nothing in the final executable.
    Jim Langston, Mar 18, 2007
    #4
  5. * arnuld:
    > Q: what does this statement do ?
    >
    > 3 + 4;
    >
    >
    > i am not able to answer it, except that it is an error


    No, it's not an error wrt. C++ language rules. Any expression is valid
    as a statement. If the expression has no side-effects (the above one
    has no side-effects) then it just doesn't have any side-effects.


    > BUT this is
    > what i got from Terminal:
    >
    > -------------- PROGRAME --------------
    > int main()
    > {
    > 3 + 4;
    >
    > return 0;
    > }
    > -------------- OUTPUT -----------------
    > [arch@voodo acc-cpp]$ g++ -ansi -pedantic -Wall -Wextra new.cpp
    > new.cpp: In function 'int main()':
    > new.cpp:3: warning: statement has no effect


    See the last line.

    In practical programming you need to watch out for that warning, because
    it might mean that you've inadvertently forgotten to supply an argument
    list in what you meant to be a function call:

    #include <iostream>
    #include <ostream>

    void foo() { std::cout << "Foo!" << std::endl; }

    int main()
    {
    foo;
    }

    --
    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, Mar 18, 2007
    #5
  6. arnuld

    Kai-Uwe Bux Guest

    arnuld wrote:

    > Q: what does this statement do ?
    >
    > 3 + 4;
    >
    >
    > i am not able to answer it, except that it is an error BUT this is
    > what i got from Terminal:
    >
    > -------------- PROGRAME --------------
    > int main()
    > {
    > 3 + 4;
    >
    > return 0;
    > }
    > -------------- OUTPUT -----------------
    > [arch@voodo acc-cpp]$ g++ -ansi -pedantic -Wall -Wextra new.cpp
    > new.cpp: In function 'int main()':
    > new.cpp:3: warning: statement has no effect
    > [arch@voodo acc-cpp]$


    Well, the compiler message says it all: the statement does nothing.

    Some background: the statement is an expression statement. An expression
    statement evaluates the expression and discards the resulting value (if
    there is one). If the resulting value is a temporary, discarding the value
    involves the destruction of the object. At the end of the statement, all
    side effects of the evaluation have taken place (this includes side effects
    from destructing temporaries). It is the side-effects that account for the
    observable behavior of an expression statement.

    Now for the example

    3 + 4;

    This evaluates 3+4 and discards the resulting 7. No side-effects, no
    observable behavior. Under the as-if rule, we can safely say that the
    statement is an elaborate version of a null-op.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 18, 2007
    #6
  7. arnuld

    arnuld Guest

    > On Mar 18, 10:40 am, "Alf P. Steinbach" <> wrote:


    > No, it's not an error wrt. C++ language rules. Any expression is valid
    > as a statement. If the expression has no side-effects (the above one
    > has no side-effects) then it just doesn't have any side-effects.


    it means it is valid C++ programme ?



    > See the last line.
    >
    > In practical programming you need to watch out for that warning, because
    > it might mean that you've inadvertently forgotten to supply an argument
    > list in what you meant to be a function call:
    >
    > #include <iostream>
    > #include <ostream>
    >
    > void foo() { std::cout << "Foo!" << std::endl; }
    >
    > int main()
    > {
    > foo;
    > }



    this is from your code:

    [arch@voodo ~]$ g++ -ansi -pedantic -Wall -Wextra new.c
    new.c: In function 'int main()':
    new.c:8: warning: statement is a reference, not call, to function
    'foo'
    new.c:8: warning: statement has no effect
    [arch@voodo ~]$

    what does 1st warning mean ?
    arnuld, Mar 18, 2007
    #7
  8. arnuld

    Jim Langston Guest

    "arnuld" <> wrote in message
    news:...
    >> On Mar 18, 10:40 am, "Alf P. Steinbach" <> wrote:

    >
    >
    >> No, it's not an error wrt. C++ language rules. Any expression is valid
    >> as a statement. If the expression has no side-effects (the above one
    >> has no side-effects) then it just doesn't have any side-effects.

    >
    > it means it is valid C++ programme ?
    >
    >
    >
    >> See the last line.
    >>
    >> In practical programming you need to watch out for that warning, because
    >> it might mean that you've inadvertently forgotten to supply an argument
    >> list in what you meant to be a function call:
    >>
    >> #include <iostream>
    >> #include <ostream>
    >>
    >> void foo() { std::cout << "Foo!" << std::endl; }
    >>
    >> int main()
    >> {
    >> foo;
    >> }

    >
    >
    > this is from your code:
    >
    > [arch@voodo ~]$ g++ -ansi -pedantic -Wall -Wextra new.c
    > new.c: In function 'int main()':
    > new.c:8: warning: statement is a reference, not call, to function
    > 'foo'
    > new.c:8: warning: statement has no effect
    > [arch@voodo ~]$
    >
    > what does 1st warning mean ?


    Exactly what it says. g++ in this case sees that you are using foo, a
    function address, as the address itself and not calling the function. The
    warning it provides is not required by the standard (AFAIK) g++ is just
    being nice. 99.44% of the time when you use a function as the adress as in
    this case it's not what you intended to do. g++ is basically saying, hey,
    you said foo; did you mean foo(); ?
    Jim Langston, Mar 18, 2007
    #8
  9. arnuld

    arnuld Guest

    > On Mar 18, 11:42 am, "Jim Langston" <> wrote:

    > Exactly what it says. g++ in this case sees that you are using foo, a
    > function address, as the address itself and not calling the function.


    you meant "foo;" is same as "*foo";

    with pointer, i mean i am taking the address of function "foo".

    > The warning it provides is not required by the standard (AFAIK)
    > g++ is just being nice.


    :)

    > 99.44% of the time when you use a function as the adress as in
    > this case it's not what you intended to do. g++ is basically saying, hey,
    > you said foo; did you mean foo(); ?


    So.. tell me this:

    "foo()" is a call for the function. does it do ask for the address ?
    arnuld, Mar 18, 2007
    #9
  10. arnuld

    Bo Persson Guest

    arnuld wrote:
    >> On Mar 18, 11:42 am, "Jim Langston" <> wrote:

    >
    >> Exactly what it says. g++ in this case sees that you are using foo,
    >> a function address, as the address itself and not calling the
    >> function.

    >
    > you meant "foo;" is same as "*foo";


    Not really.

    *foo only works if foo is a pointer to a function. Right here it is the name
    of a function. Close, but not exactly the same.

    >
    > with pointer, i mean i am taking the address of function "foo".


    Hey, you are not supposed to know about pointers until chapter 10! :)

    The expression "foo" evaluates to the address of the function foo, but is
    not using it.

    >
    >> The warning it provides is not required by the standard (AFAIK)
    >> g++ is just being nice.

    >
    > :)
    >
    >> 99.44% of the time when you use a function as the adress as in
    >> this case it's not what you intended to do. g++ is basically
    >> saying, hey, you said foo; did you mean foo(); ?

    >
    > So.. tell me this:
    >
    > "foo()" is a call for the function. does it do ask for the address ?


    It uses the address (somehow), as that is where the code is. Exactly how a
    function call is performed, is an implementation detail, outside of the
    language standard.


    Bo Persson
    Bo Persson, Mar 18, 2007
    #10
  11. arnuld

    SasQ Guest

    Dnia Sat, 17 Mar 2007 23:47:02 -0700, arnuld napisa³(a):

    > So.. tell me this:
    > "foo()" is a call for the function. does it do ask for the address ?


    When the compiler sees the following:

    foo;

    he have to know first what the name "foo" means.
    If he know it's a function name, he treats that "foo"
    as an address of that function. So in this expression statement
    the compiler evaluate that address and... do nothing with it
    [will forget it when he come to the ending semicolon].
    So it warns you that maybe you do something by an error.

    On the other hand, when the compiler sees the following:

    foo(7);

    and it knows that "foo" is a name of a function, it does the
    following:
    1. evaluates the "foo" as an address of a function "foo".
    2. evaluates "7" as an literal "in-place" value.
    3. sees that parentheses, so evaluates it as a function-call
    operator. So it calls a function from the address evaluated
    from "foo" with a parameter being literal value "7".
    4. When the function returns, the returned value replaces this
    whole expression. That value is a temporary object. It isn't
    stored anywhere, so it'll be forgotten soon.
    5. When the program execution comes to the ending semicolon, it's
    the end of a statement, so all the temporary objects are
    destructed. The value of a function call is forgotten.

    So, the "foo" alone means "address of a function 'foo'".
    The "foo" followed by parentheses means "call of a function
    'foo'". But "*foo" means referring to the memory location
    referring to by the address of function "foo".

    > you meant "foo;" is same as "*foo";
    > with pointer, i mean i am taking the address of function "foo".


    "foo" is the same as "&foo" for a function name.
    Operator * takes the address as an argument, but the result
    it evaulates to is the object [memory location] pointed to
    by that address. If you have:

    int a = 8;
    int* p = &a;

    you get:

    p a
    [0xDEADBEEF]------------->[ 8 ]

    Now, "p" is a pointer, so evaluates to an address.
    "*p" evaluates exactly as a memory location containing value "8",
    so the result will be that value. So in this case "*p" is the
    same as "a".

    std::cout << a << ' ' << p << ' ' << *p << std::endl;

    would print out:

    8 0xDEADBEEF 8

    --
    SasQ
    SasQ, Mar 18, 2007
    #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. Pete
    Replies:
    14
    Views:
    909
    Roland Pibinger
    Jan 3, 2006
  2. utab

    Accelerated C++ exercise

    utab, Feb 13, 2006, in forum: C++
    Replies:
    2
    Views:
    458
    Diego Martins
    Feb 14, 2006
  3. utab
    Replies:
    3
    Views:
    378
  4. utab
    Replies:
    8
    Views:
    517
    Default User
    Apr 16, 2006
  5. Xernoth
    Replies:
    10
    Views:
    1,044
    James Kanze
    Apr 15, 2007
Loading...

Share This Page