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

Sequence points while evaluating function arguments, Unspecified or Undefined behavio

In a function call like
Expand|Select|Wrap|Line Numbers
  1. printf("%d,..",++i,++i,..)
can we consider every ++i as a Full Expression? And if it is a full expression, it means there is a sequence point after every ++i. So, uncertainty exists only about order of evaluation of arguments and not about the side-effects of each of the ++i expressions causing Undefined behavior. I just want to know whether such function calls cause Undefined Behavior or just Unspecified Behavior. If they are undefined behaviors, please give an example for better understanding of explanation.
Thank you.

Regards,
Aftab
Mar 29 '09 #1
8 2030
JosAH
11,448 Expert 8TB
@Aftabpasha
This is what the Standard has to say about sequence points:

A.2 SEQUENCE POINTS

The following are the sequence points described in $2.1.2.3.


* The call to a function, after the arguments have been evaluated
($3.3.2.2).

* The end of the first operand of the following operators: logical
AND && ($3.3.13); logical OR || ($3.3.14); conditional ? ($3.3.15);
comma , ($3.3.17).

* The end of a full expression: an initializer ($3.5.7); the
expression in an expression statement ($3.6.3); the controlling
expression of a selection statement ( if or switch ) ($3.6.4); the
controlling expression of a while or do statement ($3.6.5); the three
expressions of a for statement ($3.6.5.3); the expression in a return
statement ($3.6.6.4).
Nowhere does it state that a function argument evaluation is a full expression evaluation so there is no sequence point after the evaluation of a single function argument.

Even more: the order of function argument evaluation is unspecified, so your example most certainly causes undefined behaviour because you are modifying a modifiable lvalue more than once before a sequence point has been reached.

kind regards,

Jos
Mar 29 '09 #2
Thanks Jos,

That means, arguments of a function call are not considered as Full expressions.

One more dumb question.

Expand|Select|Wrap|Line Numbers
  1. int i=2;
  2. i = ++i;'
In the above code, the expression i = ++i; according to the standards, should be an undefined behavior. But when I try to guess different possible outcomes of this expression, I can only think of 3 (as final value of i). How can it be undefined? Please put some light on the concept of Undefined Behavior in this case.

Thank You.

Regards,
Aftab
Mar 29 '09 #3
JosAH
11,448 Expert 8TB
@Aftabpasha
Strictly speaking (language lawyer's job) the behaviour should be undefined but because the possible outcomes of the evaluations are 3 and 3 (both the same) I'd call it defined behaviour. The Standard has gotten itself into trouble with this sequence point and undefined behavior stuff because canonical cases as you described all have a very simple (and defined!) outcome.

kind regards,

Jos
Mar 29 '09 #4
Is there a sequence point at the end of a parametrized macro, just like a function?
e.g.
Expand|Select|Wrap|Line Numbers
  1. #define SQR(x) ((x)*(x))
So when we use SQR(10); is there a sequence point concept like function call?
Mar 29 '09 #5
JosAH
11,448 Expert 8TB
@Aftabpasha
Macros are just textually substituted so if you're looking for sequence points you have to look in the macro expanded text, e.g.

Expand|Select|Wrap|Line Numbers
  1. #define FOO(x) (x)+
  2. ...
  3. int x;
  4. x=FOO(3)1; // sequence point at the semicolon
  5.  
kind regards,

Jos
Mar 30 '09 #6
Please look at the code below.

Expand|Select|Wrap|Line Numbers
  1. int x =2;
  2. printf("%d, %d, %d", ++x, ++x, ++x);
  3.  
Now, by the rules, the printf statement has a potential of Undefined behavior. With a simple guess, the possible outputs are (3, 4, 5) or (5, 4, 3) (considering the two different orders of evaluation of arguments). Now, is there an such possibility of output like (4, 3, 5) or (3, 5, 4) or (5, 3, 4) or even (GARBAGE, GARBAGE, GARBAGE) (unpredictable), and under which circumstances? Please put your views on this.
Mar 30 '09 #7
JosAH
11,448 Expert 8TB
@Aftabpasha
Undefined means undefined so anything can happen; the canonical symptom is that daemons will fly out of your nose. There is no need to discuss what actually will happen.

kind regards,

Jos
Mar 30 '09 #8
Thanks Josh for clearing my doubts.

Kind Regards,
Aftab
Mar 30 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: ...
4
by: j | last post by:
In a footnote in the c99 standard the following is labeled as undefined: a = i; And in the second clause of section 6.5 the following is stated: "Between the previous and next sequence...
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
4
by: manuscheung | last post by:
Hi all, This is not a question. Here I found a good article that discusses sequence point in details: http://www.embedded.com/shared/printableArticle.jhtml?articleID=9900661
7
by: akarl | last post by:
Hi all, Why do I get a warning from gcc with the following program? $ cat test.c #include <stdio.h> int f(int n) { return n;
15
by: Frederick Gotham | last post by:
Here's a sample function which converts a string to all uppercase: #include <assert.h> #include <ctype.h> void StringUp( char *p ) { do assert( *p >= 0 ); while( *p = toupper( *p ), *p++ );...
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
4
by: Daniel Kraft | last post by:
Hi all! I do not have a standard-document right next to me to cite from, but as far as I know, doing something like: a()=b()=c()=d(); or foo(d()+c()+b()+a()); has a fixed evaluation order...
2
by: Kai-Uwe Bux | last post by:
Please consider #include <iostream> int main ( void ) { int a = 0; a = ( (a = 5), 4 ); // (*) std::cout << a << '\n'; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.