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 <[email protected]> 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

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

#include <stdio.h>
#include <stdlib.h>

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;
}

Reply via email to