By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

Accelerated C++ - exercise 0-1

P: n/a
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]$

Mar 18 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Du~
On Mar 17, 9:29 pm, "arnuld" <geek.arn...@gmail.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.

Mar 18 '07 #2

P: n/a
On Mar 18, 10:33 am, "Du~" <aspmanualator_1234567...@yahoo.comwrote:
On Mar 17, 9:29 pm, "arnuld" <geek.arn...@gmail.comwrote:
check where it is stored.
what that means ?

your reply is beyond my understanding

:-(

Mar 18 '07 #3

P: n/a
"arnuld" <ge*********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.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.
Mar 18 '07 #4

P: n/a
* 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?
Mar 18 '07 #5

P: n/a
On Mar 18, 10:40 am, "Alf P. Steinbach" <a...@start.nowrote:

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 ?

Mar 18 '07 #6

P: n/a
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
Mar 18 '07 #7

P: n/a
"arnuld" <ge*********@gmail.comwrote in message
news:11*********************@e65g2000hsc.googlegro ups.com...
>On Mar 18, 10:40 am, "Alf P. Steinbach" <a...@start.nowrote:

>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(); ?
Mar 18 '07 #8

P: n/a
On Mar 18, 11:42 am, "Jim Langston" <tazmas...@rocketmail.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 ?

Mar 18 '07 #9

P: n/a
arnuld wrote:
>On Mar 18, 11:42 am, "Jim Langston" <tazmas...@rocketmail.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
Mar 18 '07 #10

P: n/a
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
Mar 18 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.