Accelerated C++ - exercise 0-1

A

arnuld

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

Du~

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

Jim Langston

arnuld said:
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.
 
A

Alf P. Steinbach

* 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;
}
 
K

Kai-Uwe Bux

arnuld said:
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
 
A

arnuld

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

Jim Langston

arnuld said:
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(); ?
 
A

arnuld

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

Bo Persson

arnuld said:
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.
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
 
S

SasQ

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top