473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Mar 18 '07 #1
10 1818
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
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
"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
* 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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Martin | last post by:
I am reading through Koenig and Moo's "Accelerated C++" and attempting the exercises. Are there any sample solutions somewhere? It's all very well me doing a solution, which seems to work, but for...
3
by: Frankie Montenegro | last post by:
Hi everyone, I must say that, even though I think that Accelerated C++ by Koenig and Moo is an awesome text, the wording of exercises is very poor. I spend half the time just trying to figure...
14
by: Pete | last post by:
Is anyone familiar with this book? Exercise 6-1 of Accelerated C++ asks us to reimplement the frame() and hcat() operations using iterators. I've posted my answers below, but I'm wondering if...
1
by: utab | last post by:
Hi there, I have been reading Accelerated C++ by Andrew Koenig which is an excellent way of learning C++ from even the first pages by using the standard library. One drawback is that no...
3
by: utab | last post by:
Exercise 5.10 from Accelerated C++ by Andrew Koenig Palindromes are words that are spelled the same right to left as left to right. Write a program to find all the palindromes in a dictionary....
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
10
by: Xernoth | last post by:
Hi, This is my first post here, so please be gentle. I've been studying c+ + by mostly using the book Accelerated C++ by Andrew Koenig and Barbara E. Moo. So far, I've been picking things up...
0
by: Lambda | last post by:
I'm trying to complete all the exercises of Accelerated C++. I'm not sure what does the exercise 5-5 mean. And how about 5-9? Thanks
0
by: Lambda | last post by:
It's from the Accelerated C++ exercise 8-1 The old version is based on pointer to function: double analysis(const std::vector<Student_info>& students, double analysis_grade(const Student_info&...
8
by: utab | last post by:
Dear all, In a question in the highly recommended book Accelerated C++, it is asked to change a const function into a plain function. After on the reader is expected to find which function(s)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.