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

Execution sequence with postfix increment and pass by reference

Hi,
I have a question about the execution sequence of the postfix increment
operator with respect to a function call.
void foo(int type, int*& data);

int* sequence;

foo(*sequence++, sequence);
Is the operator++ always executed before the function foo is called?
In this case the evaluation of the two arguments is independent,
otherwise the value of data in the body of foo is undefined.
Marcel

Jul 18 '08 #1
5 1788
Marcel Müller wrote:
I have a question about the execution sequence of the postfix increment
operator with respect to a function call.
void foo(int type, int*& data);

int* sequence;

foo(*sequence++, sequence);
I hope you change the value of 'sequence' to point to something valid
before the call to 'foo' here, otherwise you're dereferencing and
incrementing an uninitialised pointer.
Is the operator++ always executed before the function foo is called?
Yes, but you should not ask about when it's executed, but when the value
of the object changes. The change in the value is the side effect, and,
luckily for you, there is a sequence point before the function is called
(and after the evaluation of all arguments), so inside the function the
'sequence' already has the incremented value, and there is no undefined
behaviour.
In this case the evaluation of the two arguments is independent,
otherwise the value of data in the body of foo is undefined.
Not sure what "independent" means here. 'data' argument is a reference
that needs to be initialised. The evaluation of the second argument is
essentially a no-op.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 18 '08 #2
On Jul 18, 10:28 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
Victor Bazarov <v.Abaza...@comAcast.netwrites:
>o(*sequence++, sequence);
(and after the evaluation of all arguments), so inside the function the
'sequence' already has the incremented value, and there is no undefined
behaviour.
Possibly not indefined, but at least unspecified.
The second argument might be evaluated before the
first is being evaluated.
That doesn't change anything in the original example (which was,
I suspect, the original poster's point).
For example, an implementation might print
a
a
, another one might print
a
b
, when executing
#include <iostream>
#include <ostream>
void o( char const x, char const * const x1 )
{ ::std::cout << x << '\n';
::std::cout << *x1 << '\n'; }
int main()
{ char const * sequence = "abc";
o( *sequence++, sequence ); }
.
And a third might core dump, since this code has undefined
behavior; you're modifying sequence *and* accessing it other
than to determine the new stored value, without an intervening
sequence point.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 18 '08 #3
In article <g5**********@news.datemas.de>, v.********@comAcast.net
says...

[ ... ]
First of all, there is no such thing as "unspecified behaviour".
Oh? Did somebody remove section 1.3.13 from your copy of the standard?

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '08 #4
Victor Bazarov wrote:
Marcel Müller wrote:
>I have a question about the execution sequence of the postfix
increment operator with respect to a function call.
void foo(int type, int*& data);

int* sequence;

foo(*sequence++, sequence);

I hope you change the value of 'sequence' to point to something valid
before the call to 'foo' here, otherwise you're dereferencing and
incrementing an uninitialised pointer.
Of course.
>Is the operator++ always executed before the function foo is called?

Yes, but you should not ask about when it's executed, but when the value
of the object changes. The change in the value is the side effect, and,
luckily for you, there is a sequence point before the function is called
(and after the evaluation of all arguments), so inside the function the
'sequence' already has the incremented value, and there is no undefined
behaviour.
Thanks! That is what I wanted to know.

It makes it a bit easier to unpack ugly, proprietary structures into
reasonable objects.
Marcel
Jul 19 '08 #5
In article <7cc41178-ef83-47d5-883f-f7beb4a496fa@
25g2000hsx.googlegroups.com>, ja*********@gmail.com says...

[ ... ]
Not to suggest that Pete doesn't have a sufficient real work in
editing, but the title of the role is "project editor" for a
reason. A lot of the wording is taken verbatim from the
proposals (and a good proposal does provide exact wording).
Which means that often, the left hand doesn't know what the
right hand is doing, and it really is too much to expect Pete
(or Andy Koenig, before him, or any mortal human being) to spot
all of the inconsistencies.
Right -- I certainly didn't mean to imply that any such problem was
Pete's fault or anything like that -- only that the project editor is
(to a large extent) the only one in a good position to see the whole
picture. To a large extent, the project editor makes the changes
approved by the committee. Some proposals work very hard at maintaining
global consistency (some I've seen from Beman Dawes appear to work
particularly hard in that respect). Others just try to restrict
themselves to minimizing the possibility of introducing any new
inconsistencies by minimizing the scope of changes they make at all.

Getting back to the previous question, despite my pointing out the
definition of "unspecified behavior", the comment to which I replied
appears essentially accurate -- even though the term is defined, the
only places in the current standard that I can find the phrase
"unspecified behavior" used are 1) the definition of the term, and 2)
the table of contents entry that points at the definition of the term.

Outside of that, there are a couple hundred uses of "unspecified", but
none of them is followed immediately by "behavior", and looking at a few
dozen, there's not an easy way to change most of them to use the defined
phrase either.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 20 '08 #6

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

Similar topics

4
by: Debaser | last post by:
I was surprised to learn in class today that given a pointer p, the statement: *p++; dereferences first and then increments the pointer. So say p points to an array location array. During...
4
by: August1 | last post by:
i'm having difficulties with the execution of a program, i believe due to something i am missing in the copy constructor implementation. When i create one object of the class and run the program...
2
by: Christian Christmann | last post by:
Hi, is there a difference in terms of efficiency when using a prefix "++x;" instead of a postfix "x++;"? Thanks Chris
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
3
by: sugaray | last post by:
Can somebody explain to me what is sequence point ? With few examples would be even better. Thanx for your help.
13
by: Oleg Kharitonov | last post by:
Hello! I have found an interesting thing with postfix operator ++. What should contain the variable i after exceution the following code: int i = 5; i = i++; In VC++ 7.1 and VC++ 2005...
8
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
1
by: Scott Gifford | last post by:
Hello, I'm working on an providing an iterator interface to a database. The basic thing I'm trying to accomplish is to have my iterator read rows from the database and return constructed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.