From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA26206; Mon, 28 Apr 2003 18:32:21 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA26441 for ; Mon, 28 Apr 2003 18:32:19 +0200 (MET DST) Received: from epexch01.qlogic.org ([63.170.40.3]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h3SGWIT25807 for ; Mon, 28 Apr 2003 18:32:18 +0200 (MET DST) Received: from epmailtmp.qlogic.org ([10.20.33.254]) by epexch01.qlogic.org with Microsoft SMTPSVC(5.0.2195.5329); Mon, 28 Apr 2003 11:31:34 -0500 Received: from [10.20.33.146] ([10.20.33.146]) by epmailtmp.qlogic.org with Microsoft SMTPSVC(5.0.2195.4905); Mon, 28 Apr 2003 11:31:34 -0500 Date: Mon, 28 Apr 2003 11:40:30 -0500 (CDT) From: Brian Hurt X-X-Sender: Reply-To: Brian Hurt To: cc: OCaml List Subject: Re: [Caml-list] Looking for a real array In-Reply-To: <200304280034.25569.exa@kablonet.com.tr> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-OriginalArrivalTime: 28 Apr 2003 16:31:34.0442 (UTC) FILETIME=[A1F368A0:01C30DA3] X-Spam: no; 0.00; qlogic:01 caml-list:01 eray:01 ozkural:01 neglected:01 pointers:01 indirection:01 val:01 initialized:01 predicate:01 0.0:01 arrays:01 ocaml:01 garbage:01 mutable:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Mon, 28 Apr 2003, Eray Ozkural wrote: > I knew this all along but looks like I neglected that an array is in fact an > array of pointers rather than an array of contiguous storage blocks in > memory. Is there a way to get real FORTRAN/C arrays for people who might not > want this extra level of indirection? > > val make : int -> 'a -> 'a array > Array.make n x returns a fresh array of length n, initialized with x. All the > elements of this new array are initially physically equal to x (in the sense > of the == predicate). Consequently, if x is mutable, it is shared among all > elements of the array, and modifying x through one of the array entries will > modify all other entries at the same time. If this is a problem, you might want to use Array.init instead. Instead of doing (for instance): let r = ref 0.0 in Array.make n r which returns an array of float references, initially all r, so changing one changes all of them, instead do: Array.init n (fun _ -> ref 0.0) which creates a new reference for every element. Having a reference in addition to the data structure is a little bit of overhead. But optimization is a tricky thing- often times, what you gain in the straights you lose in the curves. For example, what happens when some other peice of code keeps a pointer to a single element of the array, when the pointer to the rest of the array- and all the other elements- are gone? In ocaml, the array and all the other elements become garbage, and the last, lone object that is not garbage stays around. Also, copying becomes a big issue in my experience. Personally, I find one extra level of dereferencing isn't that big of a deal. If you're too the point where it is a big deal, you are already talking about hand-tuned assembly language. My advice: stop worrying about minutia. Permature optimization is the root of all evil. Brian ------------------- 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