In a function call like - 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
8 2030 @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
Thanks Jos,
That means, arguments of a function call are not considered as Full expressions.
One more dumb question.
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
@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
Is there a sequence point at the end of a parametrized macro, just like a function?
e.g.
So when we use SQR(10); is there a sequence point concept like function call?
@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. -
#define FOO(x) (x)+
-
...
-
int x;
-
x=FOO(3)1; // sequence point at the semicolon
-
kind regards,
Jos
Please look at the code below. -
int x =2;
-
printf("%d, %d, %d", ++x, ++x, ++x);
-
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.
@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
Thanks Josh for clearing my doubts.
Kind Regards,
Aftab
Sign in to post your reply or Sign up for a free account.
Similar topics
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:
...
|
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...
|
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...
|
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
|
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;
|
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++ );...
|
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...
|
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...
|
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';
}
|
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...
|
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...
|
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...
|
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...
|
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...
|
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: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| | |