caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Collateral effect with pointers
@ 2004-01-21 13:07 ANDRÿffffc9 MOURA
  2004-01-21 14:38 ` skaller
  0 siblings, 1 reply; 2+ messages in thread
From: ANDRÿffffc9 MOURA @ 2004-01-21 13:07 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 3088 bytes --]

Hi to all!
 
I am trying to implement in OCaml version 3.07 the Delaunay triangulation contained in the program 'Triangle' of  J.R. Shewchuk for divide-and-conquer,  but the results differ from those produced by the 
original program. After exhausting tests, I believe that the problem is related to the structure of pointers. Or either, with some moment the values of  'farleft' and 'farright' intervene one in another one. Please, it would like to know as to hinder collateral effect (improper propagation) provoked by pointers for the following situation:
 
type 'a pointer = Null | Pointer of 'a ref;;
let ( !^ ) = function
    | Null -> invalid_arg "Attempt to dereference the null pointer"
    | Pointer r -> !r;;
(*val ( !^ ) : 'a pointer -> 'a = <fun>*)
let ( ^:= ) p v =
    match p with
    | Null -> invalid_arg "Attempt to assign the null pointer"
    | Pointer r -> r := v;;
(*val ( ^:= ) : 'a pointer -> 'a -> unit = <fun>*)
let new_pointer x = Pointer (ref x);;
(*val new_pointer : 'a -> 'a pointer = <fun>*)
...
let dummytri = new_pointer ({tr_adjtri=(Array.init 3 (fun x -> {te_ptri=Null; ta_orient=0})); tr_no=(Array.init 3 (fun x -> dummypoint)); tr_adjedg=(Array.init 3 (fun x -> dummysh)); tr_area=0.0});;

let dummypoint = new_pointer {po_x = 0.0; po_y = 0.0; po_marker = 0};;

let dummysh = new_pointer {sh_edg=(Array.init 2 (fun x -> Null)); sh_no=(Array.init 2 (fun x -> dummypoint)); sh_adjtri=(Array.init 2 (fun x -> Null)); sh_marker=0};;
let triangleloop = ref [||];;
...

...
let farleft  = ref (new_pointer {te_tri={tr_adjtri=(Array.init 3 (fun x -> {te_ptri=dummytri; ta_orient=0})); tr_no=(Array.init 3 (fun x -> dummypoint)); tr_adjedg=(Array.init 3 (fun x -> dummysh)); tr_area=0.0}; te_orient=0})
and farright = ref (new_pointer {te_tri={tr_adjtri=(Array.init 3 (fun x -> {te_ptri=dummytri; ta_orient=0})); tr_no=(Array.init 3 (fun x -> dummypoint)); tr_adjedg=(Array.init 3 (fun x -> dummysh)); tr_area=0.0}; te_orient=0}) in
...
maketriangle farleft;
maketriangle farright;
(* It has advanced, geometric primitives modify values of farleft and farright and connect they. *)
...
 

(* It adds an element to the 'triangleloop' array and returns a pointer for its last element *)
let maketriangle newtriedge =
   let l = Array.length !triangleloop in
   aumenteTriangulos triangleloop 1;
   newtriedge ^:= !triangleloop.(l);;
(*val maketriangle : triedge pointer -> unit = <fun>*)
 

(* It increases the size of  'triangleloop' *)
let aumenteTriangulos trianguloArray aumento =
   trianguloArray := Array.append !trianguloArray
                             (Array.init aumento (fun x -> {te_tri={tr_adjtri=(Array.init 3 (fun x ->
                             {te_ptri=dummytri; ta_orient=0})); tr_no=(Array.init 3 (fun x -> dummypoint));
                             tr_adjedg=(Array.init 3 (fun x -> dummysh)); tr_area=0.0}; te_orient=0}));;
(*val aumenteTriangulos : triedge array ref -> int -> unit = <fun>*)
 

Yours truly,
 
André Luiz Moura




---------------------------------
Yahoo! GeoCities: a maneira mais fácil de criar seu web site grátis!

[-- Attachment #2: Type: text/html, Size: 4205 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Collateral effect with pointers
  2004-01-21 13:07 [Caml-list] Collateral effect with pointers ANDRÿffffc9 MOURA
@ 2004-01-21 14:38 ` skaller
  0 siblings, 0 replies; 2+ messages in thread
From: skaller @ 2004-01-21 14:38 UTC (permalink / raw)
  To: ANDRÿffffc9 MOURA; +Cc: caml-list

On Thu, 2004-01-22 at 00:07, ANDRÿffffc9 MOURA wrote:

> After exhausting tests, I believe that the problem is related to the
> structure of pointers. 

In C, an assignment:

*p = *q

copies the value q points at to the location p points at.

In Ocaml, with your construction, the same thing happens
BUT there is a caveat.

In Ocaml most values are boxed, which means the
value is presented by a pointer. So suppose
in the C assignment above the value being copied
was a struct, then the whole struct would be
copied .. but not in Ocaml.

This is irrelevant for immutable data types..

> let dummytri = new_pointer ({tr_adjtri=(Array.init 3 (fun x ->
> {te_ptri=Null; ta_orient=0})); tr_no=(Array.init 3 (fun x ->
> dummypoint)); tr_adjedg=(Array.init 3 (fun x -> dummysh));
> tr_area=0.0});;

.. but Ocaml arrays are not immutable.

To copy an array, you will have to call

Array.copy

Note that of course if the elements IN the array are
also not immutable .. that still won't work
(since the array is just an array of pointers,
Array.copy just copies the pointers).

Perhaps this may be the source of your problem?


-- 
John Max Skaller, mailto:skaller@tpg.com.au
snail:25/85c Wigram Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850. Checkout Felix: http://felix.sf.net




-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-01-21 14:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-21 13:07 [Caml-list] Collateral effect with pointers ANDRÿffffc9 MOURA
2004-01-21 14:38 ` skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).