From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p7GGM5jk025630 for ; Tue, 16 Aug 2011 18:22:06 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av0EAJCYSk5QRFuw/2dsb2JhbABBqD53gUABAQUyAUYQCxgcEhQoIROHcrk7hWlfBI4OlXo X-IronPort-AV: E=Sophos;i="4.68,234,1312149600"; d="c'?scan'208";a="105605193" Received: from furbychan.cocan.org ([80.68.91.176]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/AES256-SHA; 16 Aug 2011 18:22:06 +0200 Received: from rich by furbychan.cocan.org with local (Exim 4.72) (envelope-from ) id 1QtMOz-0008UA-Pd; Tue, 16 Aug 2011 17:22:05 +0100 Date: Tue, 16 Aug 2011 17:22:05 +0100 From: "Richard W.M. Jones" To: Dmitry Bely Cc: Caml List Message-ID: <20110816162205.GC31932@annexia.org> References: <20110816152550.GA21081@annexia.org> <20110816155137.GA18365@ccellier.rd.securactive.lan> <20110816161042.GA31932@annexia.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="lrZ03NoBR/3+SXJZ" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) Subject: Re: [Caml-list] Interfacing with C: bad practice --lrZ03NoBR/3+SXJZ Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Tue, Aug 16, 2011 at 08:18:48PM +0400, Dmitry Bely wrote: > On Tue, Aug 16, 2011 at 8:10 PM, Richard W.M. Jones wrote: > > > Well it would certainly help if we had a piece of runnable code to > > look at.  The code supplied in the original email contains an infinite > > loop. > > Yes, that was my mistake that I corrected later. The loop should be > > for (; *s != NULL; s++) { > list = wrp_ml_cons(caml_copy_string(*s), list); /* bug! */ > } Well, contrary to what I said earlier, it really does seem like this is undefined C behaviour. Try the attached program, which distills the problem down to something simple. gcc with -O0 and -O2 produces completely different answers, and the assembler output confirms it. Rich. -- Richard Jones Red Hat --lrZ03NoBR/3+SXJZ Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="test.c" #include #include void f (int a, int b); int g (void); int *global = NULL; int main (void) { int x = 1; global = &x; f (g (), x); exit (0); } void f (int a, int b) { printf ("a = %d, b = %d\n", a, b); } int g (void) { if (global) { global++; return *global; } else return 42; } --lrZ03NoBR/3+SXJZ--