caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] internal representation of string
@ 2002-08-26 13:46 Oleg
  2002-08-26 17:22 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Oleg @ 2002-08-26 13:46 UTC (permalink / raw)
  To: caml-list

Hi

What is the internal representation of string? Is it basically a C-string 
[with or without terminating '\0'] plus integer storing its size? Or is it 
something more sophisticated? Also, do functions like String.sub implement 
copy-on-write mechanism or do they copy when they are called?

TIA
Oleg
-------------------
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

* Re: [Caml-list] internal representation of string
  2002-08-26 13:46 [Caml-list] internal representation of string Oleg
@ 2002-08-26 17:22 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2002-08-26 17:22 UTC (permalink / raw)
  To: Oleg; +Cc: caml-list

> What is the internal representation of string? Is it basically a C-string 
> [with or without terminating '\0'] plus integer storing its size? Or is it 
> something more sophisticated?

Like all heap blocks, strings contain a header defining the size of
the string in machine words.  The actual block contents are:
- the characters of the string
- padding bytes to align the block on a word boundary.  
  The padding is one of
    00
    00 01
    00 00 02
    00 00 00 03
  on a 32-bit machine, and up to 00 00 .... 07 on a 64-bit machine.

Thus, the string is always zero-terminated, and its length can be
computed as follows:

    number_of_words_in_block * sizeof(word) + last_byte_of_block - 1

The null-termination comes handy when passing a string to C, but is
not relied upon to compute the length (in Caml), allowing the string
to contain nulls.

> Also, do functions like String.sub implement 
> copy-on-write mechanism or do they copy when they are called?

They copy when they are called.  Caml strings really behave like
compactly-represented character arrays.

Hope this answers your question,

- Xavier Leroy
-------------------
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:[~2002-08-26 17:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-26 13:46 [Caml-list] internal representation of string Oleg
2002-08-26 17:22 ` Xavier Leroy

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).