sequence-points

Swapping Values with XOR [duplicate]

∥☆過路亽.° 提交于 2019-12-10 17:42:16
问题 This question already has answers here : Sequence Point - Xor Swap on Array get wrong result (1 answer) How does XOR variable swapping work? (9 answers) Closed 5 years ago . What is the difference between these two macros? #define swap(a, b) (((a) ^ (b)) && ((a) ^= (b) ^= (a) ^= (b))) Or #define swap(a, b) (((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b))) I saw the second macro here but couldn't understand why it wasn't written like the first one? Is there a special reason that I missed? 回答1:

Sequence Points and Method Chaining

喜欢而已 提交于 2019-12-10 12:34:23
问题 The following expression is often used to demonstrate undefined unspecified behaviour: f() + g() If f() and g() both have side effects on some shared object then the behaviour is undefined unspecified because the order of execution is unknown. f() may be evaluated before g() or vice versa. Now I was wondering what happens when you chain member functions on an object. Let's say I have an instance of a class, the instance called obj and it has two member functions, foo() and bar() which both

An explanation about Sequence points

旧时模样 提交于 2019-12-10 03:31:32
问题 Lately, I have seen a lot of questions being asked about output for some crazy yet syntactically allowed code statements like like i = ++i + 1 and i=(i,i++,i)+1; . Frankly realistically speaking hardly anyone writes any such code in actual programing.To be frank I have never encountered any such code in my professional experience. So I usually end up skipping such questions here on SO. But lately the sheer volume of such Q's being asked makes me think if I am missing out some important theory

complicated expression involving logical AND (&&)

≡放荡痞女 提交于 2019-12-10 03:19:29
问题 void main(void) { int x,y,z; x=y=z=1; z = x && y && ++z;//is this fine? } I have lately started reading about sequence points stuffs but I cannot figure out whether the above sample of code is fine or not. I know the && operator introduces a sequence point so I am not very sure about the behavior of the expression z = x && y && ++z. Someone please tell me the correct answer. 回答1: In C++ 03. void main(void) { int x,y,z; x=y=z=1; // Seq1 at ; z = x && y && ++z;//is this fine? // Seq2 at ; } NB:

Yet another sequence point query: how does *p++ = getchar() work?

做~自己de王妃 提交于 2019-12-06 07:51:28
问题 §5.1.2.4.16 EXAMPLE 7 The grouping of an expression does not completely determine its evaluation. In the following fragment: #include <stdio.h> int sum; char *p; /* ... */ sum = sum * 10 - '0' + (*p++ = getchar()); the expression statement is grouped as if it were written as sum = (((sum * 10) - '0') + ((*(p++)) = (getchar()))); but the actual increment of p can occur at any time between the previous sequence point and the next sequence point (the ; ), and the call to getchar can occur at any

Is this “*ptr++ = *ptr + a” undefined behavior?

ぐ巨炮叔叔 提交于 2019-12-06 04:13:07
问题 Well, I'm not really in serious need of this answer, I am just inquisitive. Expressions like *ptr++ = a are perfectly valid since we are operating on two objects ptr and *ptr but if i write *ptr++ = *ptr + a is it still valid ? For example consider the following snippet: int main(void){ int a[] = {5,7,8,9,2}; int* p =a; *p++ = 76; /*altering the first element */ *p++ = *p + 32; /*altering the second element */ p = a; int i; for(i = 0;i<5; i++) printf("%d ",*p++); return 0; } I think that

Why I got “operation may be undefined” in Statement Expression in C++?

本秂侑毒 提交于 2019-12-05 12:32:20
问题 to describe the problem simply, please have a look at the code below: int main() { int a=123; ({if (a) a=0;}); return 0; } I got this warning from [-Wsequence-point] Line 4: warning: operation on 'a' may be undefined my g++ version is 4.4.5 I'll appreciate whoever would explain this simple problem. btw you could find my original program and original problem in #7 in this Chinese site (not necessary) UPD1: though to change the code into ({if(a) a=0; a;}) can avoid the warning, but I recognized

Are there “sequence-point” issues with statements like “int a=4,*ptr=&a;” or “x+=4,y=x*2;”?

≯℡__Kan透↙ 提交于 2019-12-05 07:44:55
My understanding of the whole sequence points thing is basic. All I have is some crude intuitive idea that "once a sequence point is encountered, we can be sure all side effects of previous evaluations are complete". I also read that in statements like printf("%d",a++,++a,a++) the behavior is undefined as a comma doesn't signify a sequence point while a semi-colon does. So instead of making guesses and going by intuition, I feel a very rigorous and conclusive answer to this will help me a lot. So are the following kind of statements safe & certain in C: int a=4,*ptr=&a; //Statement 1 x+=4,y=x

An explanation about Sequence points

≯℡__Kan透↙ 提交于 2019-12-05 03:30:13
Lately, I have seen a lot of questions being asked about output for some crazy yet syntactically allowed code statements like like i = ++i + 1 and i=(i,i++,i)+1; . Frankly realistically speaking hardly anyone writes any such code in actual programing.To be frank I have never encountered any such code in my professional experience. So I usually end up skipping such questions here on SO. But lately the sheer volume of such Q's being asked makes me think if I am missing out some important theory by skipping such Q's. I gather that the such Q's revolve around Sequence points . I hardly know

Can a C/C++ compiler legally cache a variable in a register across a pthread library call?

妖精的绣舞 提交于 2019-12-04 19:16:17
问题 Suppose that we have the following bit of code: #include <pthread.h> #include <stdio.h> #include <stdlib.h> void guarantee(bool cond, const char *msg) { if (!cond) { fprintf(stderr, "%s", msg); exit(1); } } bool do_shutdown = false; // Not volatile! pthread_cond_t shutdown_cond = PTHREAD_COND_INITIALIZER; pthread_mutex_t shutdown_cond_mutex = PTHREAD_MUTEX_INITIALIZER; /* Called in Thread 1. Intended behavior is to block until trigger_shutdown() is called. */ void wait_for_shutdown_signal() {