473,396 Members | 2,020 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,396 software developers and data experts.

return i++

Dear all,

sorry, I always forget this thing:

Is this foo:

int foo(int i)
{
++i;
return i-1;
}

the same like this foo:

int foo(int i)
{
return i++;
}

? As I am alway scared using the ++ operators in compact forms, I'd better
ask......

Thanks,
Patrick

****************************

Reply form:

[ ] both foos are identical in the functionality and well defined
[ ] keep your fingers away from "return i++"
[ ] keep your fingers away from "return ++i"
[ ] do not know

Please mark the correct answers. More than one answer might be correct.
Jul 22 '05 #1
23 5651
Patrick Kowalzick wrote:
sorry, I always forget this thing:

Is this foo:

int foo(int i)
{
++i;
return i-1;
}

the same like this foo:

int foo(int i)
{
return i++;
}
Yes. And it really is the same as

int foo(int i)
{
return i;
}

? As I am alway scared using the ++ operators in compact forms, I'd better
ask......

Thanks,
Patrick

****************************

Reply form:

[ ] both foos are identical in the functionality and well defined
[ ] keep your fingers away from "return i++"
[ ] keep your fingers away from "return ++i"
[ ] do not know

Please mark the correct answers. More than one answer might be correct.


You better mark the first one and the last one. Both answers are correct.

V
Jul 22 '05 #2
Dear Victor,
You better mark the first one and the last one. Both answers are correct.


[x] both foos are identical in the functionality and well defined
[ ] keep your fingers away from "return i++"
[ ] keep your fingers away from "return ++i"
[x] did not know

Thanks ;),
Patrick
Jul 22 '05 #3

int Blah()
{
int k = 5;

return ++k;
}
The above function returns 6.

--

The below function returns 5.

int Blah()
{
int k = 5;

return k++;
}

--

When you put ++ before the name: The object is incremented. The value of the
expression is the object's value *after* the increment.

When you putt ++ after the name: The value of the expression is the object's
value *before* the increment. The object is incremented.
Maybe the following will enlighten?:

class Number
{
private:

unsigned k;

public:

Number& operator++()
{
//This gets called when you do ++object
//Note that it returns by reference

k += 1;

return *this;
}

Number operator++(int)
{
//This gets called when you do object++
//Note that it returns by value

//First we create a temporary, making a copy of the current object
Number temp = *this;

//Now we proceed with the increment
k += 1;

//But... we return the old value, ie. before the increment
return temp;
}
};
-JKop
Jul 22 '05 #4
When you put ++ before the name: The object is incremented. The value
of the expression is the object's value *after* the increment.

When you putt ++ after the name: The value of the expression is the
object's value *before* the increment. The object is incremented.

Here's the simple way to remember it: Just read from left to right as
normal:
++object;

[increment], then [object];
object++;

[object], then [increment];
Hopefully you're not Arabic!
-JKop
Jul 22 '05 #5
Dear JKop,

thanks a lot for your explanations. Sorry that I did not express me right,
but I know the thing about post- and pre-increment.

The only problem I have, is to remember in which cases I run into undefined
behaviour. So I'd better ask before ;). The compilers are not a good
indicator here, because some may work, and others not (undefined).

I was only *quite* sure that my code is fine, but not 100%.

Thanks,
Patrick
Jul 22 '05 #6
Patrick Kowalzick wrote:

Dear JKop,

thanks a lot for your explanations. Sorry that I did not express me right,
but I know the thing about post- and pre-increment.

The only problem I have, is to remember in which cases I run into undefined
behaviour. So I'd better ask before ;).


In a nutshell:
* make sure the variable that gets incremented is not the same variable
assigned to.

i = i++; // <- That's a no, no

* don't increment the same variable more then once in a statement

j = i++ + i++; // no, no
foo( i++, i++ ); // no, no
cout << i++ << i++; // no, no

If you don't do any of the above you should be fairly safe
(Did I miss some other common mistakes?)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7
JKop wrote:

When you put ++ before the name: The object is incremented. The value of the
expression is the object's value *after* the increment.

When you putt ++ after the name: The value of the expression is the object's
value *before* the increment. The object is incremented.


Actually, in both cases:
1. The object is incremented.
2. The value returned is either the original or
original value plus one.

You can't make any statement about WHEN the increment occurs.
Actually, I wouldn't have writen either ++k or k++.
Since I don't really care to change k, just get a value
one greater than it:

return k+1;
is MORE appropriate in my opinion.
Jul 22 '05 #8
Karl Heinz Buchegger wrote:
Patrick Kowalzick wrote:
Dear JKop,

thanks a lot for your explanations. Sorry that I did not express me right,
but I know the thing about post- and pre-increment.

The only problem I have, is to remember in which cases I run into undefined
behaviour. So I'd better ask before ;).

In a nutshell:
* make sure the variable that gets incremented is not the same variable
assigned to.

i = i++; // <- That's a no, no

* don't increment the same variable more then once in a statement

j = i++ + i++; // no, no
foo( i++, i++ ); // no, no
cout << i++ << i++; // no, no

If you don't do any of the above you should be fairly safe
(Did I miss some other common mistakes?)


Actually, variables is a bit too loose a requirement. It has to be
'object'. It is possible to increment the same object using different
variables:

int i = 42;
int& j = i;
i = j++; // same object, different variables

or even functions:

extern int i;

int foo() {
return i++;
}

int bar() {
i = foo(); // extremely obscured -- same object, and you don't
// even see the increment
}

V
Jul 22 '05 #9
Karl Heinz Buchegger wrote:
...
In a nutshell:
* make sure the variable that gets incremented is not the same variable
assigned to.

i = i++; // <- That's a no, no

* don't increment the same variable more then once in a statement

j = i++ + i++; // no, no
foo( i++, i++ ); // no, no
cout << i++ << i++; // no, no

If you don't do any of the above you should be fairly safe
(Did I miss some other common mistakes?)
...


You covered only the first part of the rule - multiple modifications
lead to UB. There's a second part as well - reading the old value for a
purpose other that calculating the new value leads to UB. For example

int i, j;
...
j = i++ + i; // no, no

Of course, it would also be useful to explain the difference between
built-in and user-defined operators (and when undefined behavior turns
into unspecified behavior) but I'm afraid we'll need a rather large
nutshell for all this.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #10
Dear Victor, dear Karl Heinz,
In a nutshell:
* make sure the variable that gets incremented is not the same variable
assigned to.

i = i++; // <- That's a no, no

* don't increment the same variable more then once in a statement

j = i++ + i++; // no, no
foo( i++, i++ ); // no, no
cout << i++ << i++; // no, no
Actually, variables is a bit too loose a requirement. It has to be
'object'. It is possible to increment the same object using different
variables:

int i = 42;
int& j = i;
i = j++; // same object, different variables

or even functions:

extern int i;

int foo() {
return i++;
}

int bar() {
i = foo(); // extremely obscured -- same object, and you don't
// even see the increment
}


thanks. I try to burn it in my brain and will ask again in 6 month.

Regards,
Patrick
Jul 22 '05 #11

Dear Ron,
Actually, I wouldn't have writen either ++k or k++.
Since I don't really care to change k, just get a value
one greater than it:

return k+1;
is MORE appropriate in my opinion.


Normaly IŽd agree, but it is a counter :) and should definitly be
incremented.

Regards,
Patrick
Jul 22 '05 #12
Victor Bazarov wrote:
...
or even functions:

extern int i;

int foo() {
return i++;
}

int bar() {
i = foo(); // extremely obscured -- same object, and you don't
// even see the increment
}
...


Although it is worth mentioning that this case is very different from
the previous ones and the behavior is perfectly defined and specified,
since the modifications of 'i' are separated from each other by at least
one sequence point.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #13
Andrey Tarasevich wrote:
Victor Bazarov wrote:
...
or even functions:

extern int i;

int foo() {
return i++;
}

int bar() {
i = foo(); // extremely obscured -- same object, and you don't
// even see the increment
And I missed a return statement here...
}
...

Although it is worth mentioning that this case is very different from
the previous ones and the behavior is perfectly defined and specified,
since the modifications of 'i' are separated from each other by at least
one sequence point.


You're absolutely right, I must have been thinking of this:

------------------------ a.cc
extern int i;
void foo(int &j) {
j = i++;
}
------------------------ b.cc
void foo(int&);
extern int i;
void bar() {
foo(i);
}

which is essentially the same as the first one, only the reference and
the object are separated from each other by a module border, while still
referring to the same object.

V
Jul 22 '05 #14

"Patrick Kowalzick" <ko*******@gmx.net> wrote in message
news:vq****************@nntpserver.swip.net...

Dear Ron,
Actually, I wouldn't have writen either ++k or k++.
Since I don't really care to change k, just get a value
one greater than it:

return k+1;
is MORE appropriate in my opinion.


Normaly I4d agree, but it is a counter :) and should definitly be
incremented.

Regards,
Patrick


If your original function (pasted below) is the way you actually have it in
code, the your "counter" isn't getting incremented, because you pass i in by
value, then increment the local copy of it. If you meant to change the
value that was passed in, make sure you pass it by reference (or pointer):

int foo(int i)
{
return i++;
}

should be:

int foo(int& i)
{
return i++;
}

-Howard

Jul 22 '05 #15
Howard wrote:
int foo(int& i)
{
return i++;
}

-Howard

or is that --Howard.
Jul 22 '05 #16

"Ron Natalie" <ro*@sensor.com> wrote in message
news:41***********************@news.newshosting.co m...
Howard wrote:
int foo(int& i)
{
return i++;
}

-Howard

or is that --Howard.


good one! :-)

Jul 22 '05 #17
DaKoadMunky wrote:
cout << i++ << i++; // no, no


I am thinking that this is unspecified behavior rather than undefined given
that the statement is really...

operator<<(operator<<(cout,i++)),i++);

The output is unspecified behavior but the modifications to i are separated by
sequence points and thus do not lead to undefined behavior.

Can anyone confirm or deny this?


Actually I once walked through all of this in the standard.
The point is:
There are 2 different forms of op<<
1) member functions of the stream
2) standalone functions

All the op<< for builtin types (int, char, long, etc...) are
member functions. Thus

cout << i++ << i++;

is equivalent to

cout.op<<( i++ ).op<<( i++ );

because i++ is of type int in both cases.

Hack I can't remember all the details, but they turned around how
function calls are handled. Which one is done first:
the evaluation of the function to call or the evaluation of the
arguments. Search google for an indepth analysis of that.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #18
int foo(int i)
{
return i++;
}

should be:

int foo(int& i)
{
return i++;
}

should be:

int foo(int& i)
{
return ++i;
}
and maybe return that by reference too...
-JKop
Jul 22 '05 #19
The ++Patrick-- got me thinking. Is any of the following legal ?

int i;

(i++)++;
++(i++);
(++i)++;
++(++i);

What happens if I omit the parnethesis ?

My guess is (++i)++ and ++(++i) is legal, because ++i is l-value.

One more thing, where can I find a little detailed information on sequence
points ? All I seem to get is very sketchy.

-Arijit
Jul 22 '05 #20
Arijit wrote:
The ++Patrick-- got me thinking. Is any of the following legal ?
What do you mean by "legal"? Well-formed? Behavior is defined?
int i;

(i++)++;
Ill-formed. Post-increment expects an lvalue.
++(i++);
Ill-formed. Pre-increment expects an lvalue.
(++i)++;
++(++i);
Both well-formed, but produce undefined behavior.
What happens if I omit the parnethesis ?
'(++i)++' without parenthesis is equivalent to '++(i++)'. Others won't
change at all.
My guess is (++i)++ and ++(++i) is legal, because ++i is l-value.


Both are well-formed, but broken, since they produce undefined behavior.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #21
>One more thing, where can I find a little detailed information on sequence
points ? All I seem to get is very sketchy.


I found this helpful...

http://www.embedded.com/story/OEG20020625S0041
Jul 22 '05 #22

"JKop" <NU**@NULL.NULL> wrote in message
news:Ky*******************@news.indigo.ie...
int foo(int i)
{
return i++;
}

should be:

int foo(int& i)
{
return i++;
}

should be:

int foo(int& i)
{
return ++i;
}


Not according to the original post, which was asking if it was ok to
post-increment, expecting the returned value to be the value prior to the
increment. Your version would return the value AFTER the increment, which
isn't what was wanted.

and maybe return that by reference too...


No need to do that, I think. It's just an integer, and wouldn't save any
time or memory doing so, would it?
--Howard

Jul 22 '05 #23
Howard posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:Ky*******************@news.indigo.ie...
int foo(int i)
{
return i++;
}

should be:

int foo(int& i)
{
return i++;
}

should be:

int foo(int& i)
{
return ++i;
}


Not according to the original post, which was asking if it was ok to
post-increment, expecting the returned value to be the value prior to
the increment. Your version would return the value AFTER the
increment, which isn't what was wanted.

and maybe return that by reference too...


No need to do that, I think. It's just an integer, and wouldn't save
any time or memory doing so, would it?
--Howard

You're right, my bad.
-JKop
Jul 22 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
20
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.