Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Win32 API
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Daniel de Rauglaudre <daniel.de_rauglaudre@i...>
Subject: Re: [Caml-list] Win32 API
Hi,

On Sun, Jun 03, 2001 at 09:01:49PM -0700, Harry Chomsky wrote:

> Now, what happens in the following situation?
>   some_function(alloc_something(), alloc_something_else());

It is unsafe. In C, you cannot call a function of two parameters if one
can modify the evaluation of the other:
      some_function(i, i++);
      some_function(a[i], i++);
In these examples, for the first parameter, you don't know which i is
concerned: the initial value of i or its ending value i + 1.

>   CAMLlocal1(v);
>   v = alloc_something();
>   some_function(v, alloc_something_else());

The rules in C are clear: the evaluation order of the parameters is
unspecified. All these calls are then illegal. You even cannot hope
that a CAMLlocal1(v) would save the value of v pushed one the stack
since in C, all parameters are transmitted by value. Therefore, you
can fix the value of v as many times you want by the CAMLlocal
feature, the pushed value will not change.

Your second example would work with a C compiler evaluating the last
parameters before the first ones.

> So the only safe solution is to go all the way:
> 
>   CAMLlocal2(v1, v2);
>   v1 = alloc_something();
>   v2 = alloc_something_else();
>   some_function(v1, v2);

Right.

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr