delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2000/08/17/20:14:38

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sources DOT redhat DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sources DOT redhat DOT com>
List-Help: <mailto:cygwin-help AT sources DOT redhat DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT sources DOT redhat DOT com
Delivered-To: mailing list cygwin AT sources DOT redhat DOT com
Message-Id: <200008180013.RAA07740@aleph.ssd.hal.com>
Subject: Re: B 20: Post incrementers in arrays as function arguments
To: bjball AT sep DOT com (Brian J Ball)
Date: Thu, 17 Aug 2000 17:13:49 -0700 (PDT)
Cc: cygwin AT sourceware DOT cygnus DOT com (Cygwin Mailing List)
In-Reply-To: <NEBBINHAMKDDGPELHGEPAEBNCAAA.bjball@sep.com> from "Brian J Ball" at Aug 17, 2000 05:06:53 PM
From: "J. J. Farrell" <jjf AT bcs DOT org DOT uk>
X-Mailer: ELM [version 2.5 PL2]
MIME-Version: 1.0

> From: "Brian J Ball" <bjball AT sep DOT com>
> 
> This code has produced 3 different results on 4 systems.

I don't know what language this code is supposed to be in, so I'll
assume it's C89.

> #include <stdio.h>
> int i;
> void do_increment(int array[]){
>    for(i;i<10;){
>       array[i++]=i;
>    }
> }
>
> void print_array(int a, int b, int c){
>   printf("%i\t%i\t%i\n",a,b,c);
> }
> 
> main(void){
>    int array[10];
>    i=0;
>    do_increment(array);
>    i=0;
>    /*  This use of var++ in a function call is
> 	 unpredictable */
>    print_array(array[i++],array[i++],array[i++]);
> }

Not only unpredictable but also undefined. The compiler can choose
to refuse to compile this code if it wishes, or it can produce a
program which behaves in some arbitrary way, or anything else it
feels like doing.

> The only workarround is to know how your system behaves or define temp
> variables.

Correct. The program you give is not valid C89. The best workaround
is to re-write it in valid C so its behaviour is predictable on all
C compilers.

> However, this works on some platforms and could become an error in porting
> software.

It works on all platforms, by definition, since the platform is
allowed to do anything it fancies when it compiles this code!


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019