Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Runtime string allocation/resizing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-06-21 (20:43)
From: Robert Roessler <roessler@r...>
Subject: Runtime string allocation/resizing
When dealing with OCaml-to-C-land interfacing, I sometimes am in the 
position of needing to allocate and fill a string - but only have an 
upper bound for the length... until after I have already copied the 
data [from a third-party library function].

What is the recommended "OCaml" way to deal with this?

Should I do two caml_alloc_string calls?  One to initially "get" the 
data, and then alloc a second one and copy to it after I know the 
correct length?

Or would it be considered better practice to use malloc in the C heap 
for the "temp" copy and only do the caml_alloc_string when I know the 
exact size?

Or ?

BTW, I am more concerned with longer-term issues like memory 
fragmentation and "extra" [minor] collections than with raw speed (as 
in copying the data twice being "evil").

Robert Roessler