freelanceprogrammers.org Forum Index » C

Re: Pre/Post the Devil


View user's profile Post To page top
johnmatthews... Posted: Mon Jul 28, 2003 8:14 pm


Joined: 28 Jul 2003

Posts: 3
Re: Pre/Post the Devil
Hi

Using gcc on my Solaris machine:

-----Original Message-----
From: Navin [mailto:navinseshadri@...]

> int i = 10;
> printf("The output is [%d]
",i-- * i++);

The output is [110]

> int i = 10;
> printf("%d,%d,%d,%d",i++,++i,i--,--i);

10,12,12,10

But of course because the results are undefined according to the C language,
I might get different output tomorrow.

It might be interesting working out why a particular compiler gives the
answer it does for undefined code like this, but if your job is writing
correct code that will always give the right answer, it doesn`t help you. It
would be better to work out how to correct the code.

Regards
John
Reply with quote
Send private message
View user's profile Post To page top
ranjith@... Posted: Tue Jul 29, 2003 8:46 am


Joined: 29 Jul 2003

Posts: 6
Re: Pre/Post the Devil
Hai All
This is to clarify how & what matters when we use pre/post in an
expression.
I am giving the details of sequence point,importance of sequence point
and implementation-defined, unspecified, and undefined behavior
termrinologies.

All the details are from FAQ.
Hope it will be useful.

Thanks
Ranjith T




Section 3. Expressions

3.1: Why doesn`t this code:

a[i] = i++;

work?

A: The subexpression i++ causes a side effect -- it modifies i`s
value -- which leads to undefined behavior since i is also
referenced elsewhere in the same expression. (Note that
although the language in K&R suggests that the behavior of this
expression is unspecified, the C Standard makes the stronger
statement that it is undefined -- see question 11.33.)

References: K&R1 Sec. 2.12; K&R2 Sec. 2.12; ANSI Sec. 3.3; ISO
Sec. 6.3.



3.2: Under my compiler, the code

int i = 7;
printf("%d
", i++ * i++);

prints 49. Regardless of the order of evaluation, shouldn`t it
print 56?

A: Although the postincrement and postdecrement operators ++ and --
perform their operations after yielding the former value, the
implication of "after" is often misunderstood. It is *not*
guaranteed that an increment or decrement is performed
immediately after giving up the previous value and before any
other part of the expression is evaluated. It is merely
guaranteed that the update will be performed sometime before the
expression is considered "finished" (before the next "sequence
point," in ANSI C`s terminology; see question 3.8). In the
example, the compiler chose to multiply the previous value by
itself and to perform both increments afterwards.

The behavior of code which contains multiple, ambiguous side
effects has always been undefined. (Loosely speaking, by
"multiple, ambiguous side effects" we mean any combination of
++, --, =, +=, -=, etc. in a single expression which causes the
same object either to be modified twice or modified and then
inspected. This is a rough definition; see question 3.8 for a
precise one, and question 11.33 for the meaning of "undefined.")
Don`t even try to find out how your compiler implements such
things (contrary to the ill-advised exercises in many C
textbooks); as K&R wisely point out, "if you don`t know *how*
they are done on various machines, that innocence may help to
protect you."

References: K&R1 Sec. 2.12 p. 50; K&R2 Sec. 2.12 p. 54; ANSI
Sec. 3.3; ISO Sec. 6.3; CT&P Sec. 3.7 p. 47; PCS Sec. 9.5 pp.
120-1.


3.8: How can I understand these complex expressions? What`s a
"sequence point"?

A: A sequence point is the point (at the end of a full expression,
or at the ||, &&, ?:, or comma operators, or just before a
function call) at which the dust has settled and all side
effects are guaranteed to be complete. The ANSI/ISO C 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.

The second sentence can be difficult to understand. It says
that if an object is written to within a full expression, any
and all accesses to it within the same expression must be for
the purposes of computing the value to be written. This rule
effectively constrains legal expressions to those in which the
accesses demonstrably precede the modification.

See also question 3.9 below.

References: ANSI Sec. 2.1.2.3, Sec. 3.3, Appendix B; ISO
Sec. 5.1.2.3, Sec. 6.3, Annex C; Rationale Sec. 2.1.2.3; H&S
Sec. 7.12.1 pp. 228-9.



3.9: So given

a[i] = i++;

we don`t know which cell of a[] gets written to, but i does get
incremented by one.

A: *No.* Once an expression or program becomes undefined, *all*
aspects of it become undefined. See questions 3.2, 3.3, 11.33.





11.33: People seem to make a point of distinguishing between
implementation-defined, unspecified, and undefined behavior.
What`s the difference?

A: Briefly: implementation-defined means that an implementation
must choose some behavior and document it. Unspecified means
that an implementation should choose some behavior, but need not
document it. Undefined means that absolutely anything might
happen. In no case does the Standard impose requirements; in
the first two cases it occasionally suggests (and may require a
choice from among) a small set of likely behaviors.

Note that since the Standard imposes *no* requirements on the
behavior of a compiler faced with an instance of undefined
behavior, the compiler can do absolutely anything. In
particular, there is no guarantee that the rest of the program
will perform normally. It`s perilous to think that you can
tolerate undefined behavior in a program; see question 3.2 for a
relatively simple example.

If you`re interested in writing portable code, you can ignore
the distinctions, as you`ll want to avoid code that depends on
any of the three behaviors.

See also questions 3.9

References: ANSI Sec. 1.6; ISO Sec. 3.10, Sec. 3.16, Sec. 3.17;
Rationale Sec. 1.6.



----- Original Message -----
From: "Matthews, John" <john.matthews@...>
To: <Programmers-Town@yahoogroups.com>
Sent: Monday, July 28, 2003 8:44 PM
Subject: RE: *(&Programmers-Town&)* Pre/Post the Devil


> Hi
>
> Using gcc on my Solaris machine:
>
> -----Original Message-----
> From: Navin [mailto:navinseshadri@...]
>
> > int i = 10;
> > printf("The output is [%d]
",i-- * i++);
>
> The output is [110]
>
> > int i = 10;
> > printf("%d,%d,%d,%d",i++,++i,i--,--i);
>
> 10,12,12,10
>
> But of course because the results are undefined according to the C
language,
> I might get different output tomorrow.
>
> It might be interesting working out why a particular compiler gives the
> answer it does for undefined code like this, but if your job is writing
> correct code that will always give the right answer, it doesn`t help you.
It
> would be better to work out how to correct the code.
>
> Regards
> John
>
>
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help