473,698 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What does f(x++) mean?

What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
3. undefined behavior, it is different for different compilers

I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.
Nov 13 '05 #1
11 7613
On Fri, 03 Oct 2003 07:07:41 -0700, becte wrote:
What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
3. undefined behavior, it is different for different compilers

I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.
I just happened to read a similar question yesterday in google,
dating from 1982. Dennis Ritchie replied:
From: research!dmr
Date: Mon Dec 27 06:19:21 1982
Subject: Pure and applied C

John Levine wondered whether, in the call bar(x++) with x global,
bar would see the old or the incremented value of x. The parameter
is clearly the old value; the question is whether the compiler might
be allowed to do the increment after the call. ... Steve Johnson brought up this very
point when he was writing PCC, and I said that as far as I knew he was
entitled to delay the increment. We agreed, though, that the result
might surprise people, and he decided not to rock the boat.


As for today, 6.5.2.4.2 states

The result of the postfix ++ operator is the value of the operand.

so, the value passed as an argument to f() should definitely be
the value before incrementing.

6.5.2.4.2 continues:

The side effect of updating the stored value of the operand shall
occur between the previous and the next sequence point.

That combined with C.1:

The following are the sequence points described in 5.1.2.3:
-- The call to a function, after the arguments have been evaluated.
Seems to indicate that the value of x must be updated before the
f() is called, not after, as you claim in (2). I compiled a test
program with GCC (3.3.1) to investigate its behavior:

#include <stdio.h>

int x = 0;

void f (int y)
{
printf("global in function: %d\n", x);
printf("param in function: %d\n", y);
}

int main (void)
{
printf("before function: %d\n", x);
f(x++);
printf("after function: %d\n", x);
return 0;
}
The results were:

before function: 0
global in function: 1
param in function: 0
after function: 1

- Sheldon

Nov 13 '05 #2
"becte" <he************ ***@hotmail.com > wrote:
What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
Absolutely wrong. This would be a preincrement effect, not postincrement.
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
Close. The value of x before the ++ is sent to f. The variable x is
also actually incremented before f is called, because there is a
sequence point at the call to f. This does not affect the value
sent to f.
3. undefined behavior, it is different for different compilers
No, the behaviour is not undefined.
I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.


gcc gave you number 1 above? It doesn't for me. I get behaviour like
what I described above for three different compilers:

C:\prog\c>gcc henrik.c -o henrik_gcc

C:\prog\c>henri k_gcc
i = 0
called f(0) but now i = 1
i = 1

C:\prog\c>lc henrik.c -o henrik_lc.exe

C:\prog\c>henri k_lc
i = 0
called f(0) but now i = 1
i = 1

C:\prog\c>bcc32 -ehenrik_bcc.exe henrik.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
henrik.c:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

C:\prog\c>henri k_bcc
i = 0
called f(0) but now i = 1
i = 1

C:\prog\c>type henrik.c
#include <stdio.h>

int i;

void f(int a)
{
printf("called f(%d) but now i = %d\n", a, i);
}

int main(void)
{
printf("i = %d\n", i);
f(i++);
printf("i = %d\n", i);
return 0;
}

--
Simon.
Nov 13 '05 #3
becte wrote:
What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
This one if f is a function. I could be wrong, but I don't think there
is any undefined behavior. 3. undefined behavior, it is different for different compilers
This one if f is a macro.
I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.


Nov 13 '05 #4
"Noah Roberts" <nr******@donte mailme.com> wrote:
becte wrote:
What does the standard say about f(x++) ? [snip] 3. undefined behavior, it is different for different compilers


This one if f is a macro.


Undefined behaviour if f is a macro?

#define f(i) printf("%d\n", i)

f(x++);

Where is the undefined behaviour?

--
Simon.
Nov 13 '05 #5
>
Where is the undefined behaviour?

--
Simon.


I believe he was considering macros that make multiple accesses to a
parameter within a single sequence point.

DrX

Nov 13 '05 #6
Thanks for the answers. Maybe I was oversimplifying a little bit.
The actual code looked like this

// gcc seems to be "wrong" here

listElement_s *listElement = &listElement[index];
// ...
memmove ((char *) listElement,
(char *) &list[index + 1],
sizeof (listElement_s) *
(noOfElements-- - index-- - 1));

The idea here is to "pack" a list of elements, i.e. remove an
element somewhere in the interior of the list
(listElement points to &list[index]) and
at the same time decrement noOfElements (and index).

In AIX (xlC) this works OK, but in Linux(gcc) the list is not "packed"
but noOfRoutePoints and routePointEleme ntIndexWork are decreased.

To make it work in Linux I had to rewrite the code:

memmove ((char *) listElement,
(char *) &list[index + 1],
sizeof (listElement_s) *
(noOfElements - index - 1));
noOfElements--;
index--;

I assumed that the problem was that the decremented value of index
was used in memmove, so that index + 1 --> index. Then the
first and second arguments are equal and the list is not changed at all.
Nov 13 '05 #7

On Fri, 3 Oct 2003, becte wrote:

Thanks for the answers. Maybe I was oversimplifying a little bit.
The actual code looked like this

// gcc seems to be "wrong" here

listElement_s *listElement = &listElement[index];
// ...
memmove ((char *) listElement,
(char *) &list[index + 1],
sizeof (listElement_s) *
(noOfElements-- - index-- - 1));
This argument by itself is okay; but in combination with
the other arguments, which also try to use the value of
'index', it invokes undefined behavior. IOW, it's perfectly
well-defined and okay to write

foo(x++)

but it's entirely wrong and bad to write

foo(x, x++)

or

foo(x++, x)

or any combination of x++ and x without a sequence point
in between them. The ',' separator between function arguments
is *not* a sequence point, even though the comma operator (also
',', but in a different context) *is* a sequence point.

In AIX (xlC) this works OK, but in Linux(gcc) the list is not "packed"
but noOfRoutePoints and routePointEleme ntIndexWork are decreased.
Yup. Undefined behavior will do that to you.
To make it work in Linux I had to rewrite the code:

memmove ((char *) listElement,
(char *) &list[index + 1],
sizeof (listElement_s) *
(noOfElements - index - 1));
noOfElements--;
index--;
That works.
I assumed that the problem was that the decremented value of index
was used in memmove, so that index + 1 --> index. Then the
first and second arguments are equal and the list is not changed at all.


It's undefined behavior; anything can happen.
Your fixed code is fine. Go with it. (Should've been
written that way from the start.)

-Arthur

Nov 13 '05 #8
To clarify again; I checked your test programs and they work fine. E.g.

#include <stdio.h>

int f(int x)
{
printf ("Local x = %d\n",x);
return 0;
}

int main()
{
int i = 10;

f(i--);
return 0;
}

will return 10 in with gcc as well. So this simple case is OK.
The problem occurs only in the more complicated problem
with memmove.
Nov 13 '05 #9
"Simon Biber" <ne**@ralminNOS PAM.cc> wrote in message news:<3f******* *************** *@news.optusnet .com.au>...
"Noah Roberts" <nr******@donte mailme.com> wrote:
becte wrote:
What does the standard say about f(x++) ? [snip] 3. undefined behavior, it is different for different compilers


This one if f is a macro.


Undefined behaviour if f is a macro?

#define f(i) printf("%d\n", i)

f(x++);

Where is the undefined behaviour?


Presumably he meant that it *may* give undefined (or unspecified)
behaviour if f is a macro, eg
#define f(i) ((i)-(i))
#define f(i) (g(i)-g(i))
-thomas
Nov 13 '05 #10

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

Similar topics

125
14756
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
3
29677
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification says: "The 'visibility' property takes the value 'collapse' for row, row group, column, and column group elements. This value causes the entire row or column to be removed from the display, and the space normally taken up by the row or column to...
86
7770
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that: http://www.clagnut.com/blog/348/ I hope that's going to be a good discussion! Michael
44
4214
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there must be many know the answer here. thanks
2
10213
by: Steve Richter | last post by:
What does the "." mean in the following sql script stmts? use GO if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table . GO
121
10080
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
51
4526
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
1
8739
by: Frank Rizzo | last post by:
Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe." What exactly does this mean? Does this mean that if I call a shared method from 2 different threads, nothing whacky will happen? Also when it says that instance members...
13
5040
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
550
by: JoeC | last post by:
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; What does this mean? I have seen v=&var->member.thing; but what does it mean when you change the & for int?
0
8675
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.