sequence-points

Ternary operator and Sequence Points in C

喜你入骨 提交于 2019-12-18 04:54:18
问题 I've an expression of the form shown below :- while (count) { ... ... index = ((count == 20)? 0 : index++); ... ... } Now Ternary operators are sequence points in C but I believe that the sequence point ends at the test part. Is this understanding correct and as such will this statement lead to undefined behaviour ? 回答1: Right. There's a sequence point after the evaluation of the condition, but the next sequence point is the semicolon terminating the statement. So whenever count != 20 , you

Sequence points and side effects in C

大兔子大兔子 提交于 2019-12-17 07:51:06
问题 In this C-FAQ it is give about sequence point; The Standard states that; Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. In the examples i = i++; a[i] = i++; it is clear from first sentence of the statement that these examples are results in undefined behavior . In explaining second sentence of the statement it is

Sequence points and side effects in C

流过昼夜 提交于 2019-12-17 07:50:52
问题 In this C-FAQ it is give about sequence point; The Standard states that; Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. In the examples i = i++; a[i] = i++; it is clear from first sentence of the statement that these examples are results in undefined behavior . In explaining second sentence of the statement it is

In C99, is f()+g() undefined or merely unspecified?

核能气质少年 提交于 2019-12-17 07:23:09
问题 I used to think that in C99, even if the side-effects of functions f and g interfered, and although the expression f() + g() does not contain a sequence point, f and g would contain some, so the behavior would be unspecified: either f() would be called before g(), or g() before f(). I am no longer so sure. What if the compiler inlines the functions (which the compiler may decide to do even if the functions are not declared inline ) and then reorders instructions? May one get a result

Explaination for printf with comparing variables as arguments

女生的网名这么多〃 提交于 2019-12-13 22:33:07
问题 main(){ int a = 5; int b = 6; printf("%d %d %d",a==b,a=b,a<b); } Output in my testing 1 6 1 In above program I am expecting output as 0 6 0 . In some compilers it is giving this output (e.g. Xcode) but where as in some other compilers it is giving output as 1 6 1 . I couldn't find the explanation . It is also the case of Sequence point. Consider this below program main(){ int a = 5; int b = 6; printf("%d %d %d",a<b,a>b,a=b); printf("%d %d",a<=b,a!=b); } Output in my testing 0 0 6 1 0 this

Confusing answers : One says *myptr++ increments pointer first,other says *p++ dereferences old pointer value

我怕爱的太早我们不能终老 提交于 2019-12-13 21:19:20
问题 I would appreciate if you clarify this for me.Here are two recent questions with their accepted answers: 1) What is the difference between *myptr++ and *(myptr++) in C 2) Yet another sequence point query: how does *p++ = getchar() work? The accepted answer for the first question,concise and easily to understand states that since ++ has higher precedence than * , the increment to the pointer myptr is done first and then it is dereferenced.I even checked that out on the compiler and verified it

Undefined behavior and sequence points

拈花ヽ惹草 提交于 2019-12-12 17:09:39
问题 What are "sequence points"? What is the relation between undefined behaviour and sequence points? I often use funny and convoluted expressions like a[++i] = i; , to make myself feel better. Why should I stop using them? If you've read this, be sure to visit the follow-up question Undefined behavior and sequence points reloaded . (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that

Suggestions for concise index handling in circular buffer

余生颓废 提交于 2019-12-11 03:18:26
问题 I've implemented a circular buffer, and I would like a concise means of updating the buffer pointer while properly handling the wrap-around. Assuming an array of size 10, my first response was something like: size_t ptr = 0; // do some work... p = ++p % 10; Static analysis, as well as gcc -Wall -Wextra , rightly slapped my wrist for unspecified behavior due to a sequence point violation. The obvious fix is something like: p++; p %= 10; However, I was looking for something more concise, (i.e.,

std::initializer_list and order of evaluation of the elements [duplicate]

风格不统一 提交于 2019-12-11 02:08:13
问题 This question already has answers here : Are multiple mutations within initializer lists undefined behavior? (2 answers) Closed 6 years ago . Is the comma ( , ) a sequence point in std::initializer_list ? example: is this UB or not: #include <vector> int main() { auto nums = [] { static unsigned x = 2; return ( x++ % 2 ) + 1; }; std::vector< int > v{ nums(), nums(), nums(), nums(), nums() }; // not sure if this is different: (note the additional brackets) // std::vector< int > v({ nums(),

operation on 'i' may be undefined [duplicate]

怎甘沉沦 提交于 2019-12-10 23:38:32
问题 This question already has answers here : Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 5 years ago . I have this code to take a string of the form bla_2 and separate it: void separate(char* str, char* word, int* n) { int i = 0; while(str[i] != '_') { word[i] = str[i++]; } *n = str[++i] - '0'; } I got: warning: operation on ‘i’ may be undefined [-Wsequence-point] But I am only changing i via ++ operator, I am not assigning anything to. So, why is