https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89899
Bug ID: 89899 Summary: g++ compiler error report Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: hos.lee at samsung dot com Target Milestone: --- Dear, I have a question about g++ compiler when running the below example. [ used compiler ] - Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-17134-Microsoft x86_64) - g++ test.cpp -lpthread There is a "test program" I checked in the below. With next statement in test program, error is occurred. - que[head++] = data; However, with next statement in test program, error is not occurred. - que[head] = data; head++; "que[head++] = data;" and "que[head] = data; head++;" is exactly same. I confused why "que[head++] = data;" statement happened error only. I also checked FAQ below and I do not find why this error is happened. Please check this bug report and feedback to me. Thank you very much. [ Test Program ] --------------------------------------------------------------------- #include <stdio.h> #include <pthread.h> volatile int DONE = 0; volatile int que[65536]; volatile unsigned short head = 0; volatile unsigned short tail = 0; void in(int data) { while ((tail - head == 1) || (head - tail == 65535)); que[head++] = data; // que[head] = data; // head++; } int out(void) { while (head == tail); return que[tail++]; } void* test0(void* param) { for (int c = 0; c < 10000000; c++) in(c); putchar('0'); DONE++; } void* test1(void* param) { for (int c = 0; c < 10000000; c++) if (out() != c) putchar('E'); putchar('1'); DONE++; } int main(void) { pthread_t thread1; pthread_t thread2; pthread_create(&thread1, NULL, test0, (void*)0); pthread_create(&thread2, NULL, test1, (void*)0); while (DONE < 2); } ------------------------------------------------------------------------ [ Frequently Reported Bugs ] Increment/decrement operator (++/--) not working as expected - a problem with many variations. The following expressions have unpredictable results: x[i]=++i foo(i,++i) i*(++i) /* special case with foo=="operator*" */ std::cout << i << ++i /* foo(foo(std::cout,i),++i) */ since the i without increment can be evaluated before or after ++i. The C and C++ standards have the notion of "sequence points". Everything that happens between two sequence points happens in an unspecified order, but it has to happen after the first and before the second sequence point. The end of a statement and a function call are examples for sequence points, whereas assignments and the comma between function arguments are not. Modifying a value twice between two sequence points as shown in the following examples is even worse: i=++i foo(++i,++i) (++i)*(++i) /* special case with foo=="operator*" */ std::cout << ++i << ++i /* foo(foo(std::cout,++i),++i) */ This leads to undefined behavior (i.e. the compiler can do anything).