Accelerated C++ - exercise 0-1

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arnuld

    Accelerated C++ - exercise 0-1

    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]$

  • Du~

    #2
    Re: Accelerated C++ - exercise 0-1

    On Mar 17, 9:29 pm, "arnuld" <geek.arn...@gm ail.comwrote:
    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.

    Comment

    • arnuld

      #3
      Re: Accelerated C++ - exercise 0-1

      On Mar 18, 10:33 am, "Du~" <aspmanualator_ 1234567...@yaho o.comwrote:
      On Mar 17, 9:29 pm, "arnuld" <geek.arn...@gm ail.comwrote:
      check where it is stored.
      what that means ?

      your reply is beyond my understanding

      :-(

      Comment

      • Jim Langston

        #4
        Re: Accelerated C++ - exercise 0-1

        "arnuld" <geek.arnuld@gm ail.comwrote in message
        news:1174195766 .131493.153210@ l77g2000hsb.goo glegroups.com.. .
        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.


        Comment

        • Alf P. Steinbach

          #5
          Re: Accelerated C++ - exercise 0-1

          * 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?

          Comment

          • arnuld

            #6
            Re: Accelerated C++ - exercise 0-1

            On Mar 18, 10:40 am, "Alf P. Steinbach" <a...@start.now rote:

            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 ?

            Comment

            • Kai-Uwe Bux

              #7
              Re: Accelerated C++ - exercise 0-1

              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

              Comment

              • Jim Langston

                #8
                Re: Accelerated C++ - exercise 0-1

                "arnuld" <geek.arnuld@gm ail.comwrote in message
                news:1174196932 .527962.65800@e 65g2000hsc.goog legroups.com...
                >On Mar 18, 10:40 am, "Alf P. Steinbach" <a...@start.now rote:
                >
                >
                >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(); ?


                Comment

                • arnuld

                  #9
                  Re: Accelerated C++ - exercise 0-1

                  On Mar 18, 11:42 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
                  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 ?

                  Comment

                  • Bo Persson

                    #10
                    Re: Accelerated C++ - exercise 0-1

                    arnuld wrote:
                    >On Mar 18, 11:42 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
                    >
                    >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


                    Comment

                    • SasQ

                      #11
                      Re: Accelerated C++ - exercise 0-1

                      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

                      Comment

                      Working...