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. 11 7582
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
"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>henrik_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>henrik_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>henrik_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.
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.
"Noah Roberts" <nr******@dontemailme.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.
> 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
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 routePointElementIndexWork 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.
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 routePointElementIndexWork 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
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.
"Simon Biber" <ne**@ralminNOSPAM.cc> wrote in message news:<3f***********************@news.optusnet.com. au>... "Noah Roberts" <nr******@dontemailme.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
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi***********************************@unix44 .andrew.cmu.edu>... 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.
This answers my question.
I wrote a small test program:
#include <stdio.h>
int i = 10;
int f(int a, int b)
{
printf ("a = %d, b = %d, Global i = %d\n", a, b, i);
return 0;
}
int main()
{
f(i, i--);
return 0;
}
In AIX (xlC) I get
a = 10, b = 10, Global i = 9
In Linux (gcc) I get
a = 9, b = 10, Global i = 9
"becte" <he***************@hotmail.com> wrote in message
news:b4**************************@posting.google.c om... "Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message
news:<Pi***********************************@unix44 .andrew.cmu.edu>... 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++)
^^^^^^^^^^^
f(i, i--) is no different. 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.
This answers my question. I wrote a small test program:
You can't test undefined behaviour.
#include <stdio.h>
int i = 10;
int f(int a, int b) { printf ("a = %d, b = %d, Global i = %d\n", a, b, i); return 0; }
int main() { f(i, i--);
The behaviour of the line is not defined by C. That doesn't merely mean that
the result is not specified, it means that *anything* can happen, including
a subsequent formatting of your harddisk.
return 0; }
In AIX (xlC) I get a = 10, b = 10, Global i = 9 In Linux (gcc) I get a = 9, b = 10, Global i = 9
Your test program is flawed, so it can't demonstrate anything.
In any case, the mere observation that the same program generates different
results on two different implementations does not prove the program is in
error.
#include <stdio.h>
int f(void) { puts("f()"); return 42; }
int g(void) { puts("g()"); return 42; }
int main(void)
{
f() + g();
return 0;
}
This is a legitimate C program which can output one of two possible results.
But the language doesn't specify which one.
Expressions like (i + i++) and foo(i, i++) fall in a much worse category of
behaviour: undefined.
--
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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:...
|
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...
|
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 ....
|
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...
|
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...
|
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...
|
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
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
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...
|
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,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |