[comp.lang.c.moderated] Interaction between global pointers and function return values

[comp.lang.c.moderated] Interaction between global pointers and function return values

Post by Ersek, Las » Wed, 13 Jan 2010 15:05:48


From: "Andris K," < XXXX@XXXXX.COM >
Date: Mon, 11 Jan 2010 18:26:01 -0600 (CST)
Message-ID: < XXXX@XXXXX.COM >


The above expression statement contains two sequence points:

1) C89 6.3.2.2 Function calls:

"The order of evaluation of the function designator, the arguments,
and subexpressions within the arguments is unspecified, but there
is a sequence point before the actual call."

2) C89 6.6 Statements

"A full expression is an expression that is not part of another
expression. Each of the following is a full expression: [...] the
expression in an expression statement [...] The end of a full
expression is a sequence point."

The program contains undefined behavior.

C89 6.3 Expressions

"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."

The second phrase is violated. Consider the following (partial) order of
evaluation:

a) sequence point 1 is reached,
b) pcptr is evaluated on the left hand side of the assignment operator,
c) function_A() is called,
d) sequence point 2 is reached (the semicolon at the end of line).

In step b, "pcptr" is evaluated for a different purpose than establishing
its next stored value (which happens in step c). Therefore, the compiler
sees no functional dependency between those steps, and since no sequence
point intervenes either, the compiler can order those steps (even
parallelize them) as it likes.



I don't think so.



It's not astonishing in my opinion, they are free to. The program must be
fixed, for example:

{
code_type code;

code = function_A();
/* modification of pcptr complete here */
pcptr->code = code;
}

But rather eliminate the global variable pcptr and/or rewrite
function_A().

(I hope I didn't botch this up too much.)

Cheers,
lacos
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

[comp.lang.c.moderated] Interaction between global pointers and function return values

Post by Tim Rentsc » Thu, 14 Jan 2010 12:59:21

"Ersek, Laszlo" < XXXX@XXXXX.COM > writes:


Unspecified behavior, not undefined. The value access of pcptr
must happen either completely before or completely after the
function call. It's still a bad idea to write code like this,
but it's only unspecified behavior, not undefined behavior.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.