Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!ucsd!mvb.saic.com!ncr-sd!iss-rb!tortuga!ewing From: ewing@tortuga.SanDiego.NCR.COM (David Ewing) Newsgroups: comp.sys.amiga.programmer Subject: Re: ANOTHER SAS C BUG Summary: This is not a bug !!! Keywords: sequence points, side effects Message-ID: <1991Apr1.224954.12574@SanDiego.NCR.COM> Date: 1 Apr 91 22:49:54 GMT References: <1991Mar31.035009.13183@csis.dit.csiro.au> Sender: @SanDiego.NCR.COM Reply-To: ewing@tortuga.SanDiego.NCR.COM (David Ewing) Organization: NCR Corporation, Rancho Bernardo Lines: 70 In article <1991Mar31.035009.13183@csis.dit.csiro.au> dave@csis.dit.csiro.au (David Campbell) writes: >/* Here is some source which demonstrates a bug in the SAS C compiler */ > >#include > >char *str="hello\n"; > >main() { > char *ptr; > ptr = str; > *ptr = '\001' + *ptr++; > printf(str); >} >/* Should print "iello" through the incrementing of str[0] > Instead prints "hillo" This is not a bug in SAS C, as a careful reading of the ANSI standard will confirm. The behavior of your code is clearly undefined. From the standard : " 2.1.2.3 Program Execution ... Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place... 3.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. (footnote 34) ... the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.... footnote 34. This paragraph renders undefined statement expressions such as i = ++i + 1; ..." Also see appendix B on sequence points. This is nothing new to ANSI C. Kernighan and Ritchie warn : "When side effects (assignment to actual variables) takes place is left to the discretion of the compiler, since the best order strongly depends on machine architecture." Furthermore, as another astute reader has already commented, modifying char *str="hello\n"; is also non-portable and undefined. This should be char str[] = "hello\n"; --> "3.5.7 Initialization ... char s[] = "abc" ... The contents of the array are modifiable. On the other hand, the declaration char *p = "abc"; defines p with type 'pointer to char' that is initialized to point to an object with type 'array of char' with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p to modify the contents of the array, the behavior is undefined." Please take the time to THOROUGHLY understand C before you disparage a compiler. ********************************************* David A. Ewing ewing@tortuga.sandiego.ncr.com *********************************************