caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <xavier.leroy@inria.fr>
To: Oleg <oleg_inconnu@myrealbox.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] internal representation of string
Date: Mon, 26 Aug 2002 19:22:02 +0200	[thread overview]
Message-ID: <20020826192202.A6666@pauillac.inria.fr> (raw)
In-Reply-To: <200208261344.JAA23445@dewberry.cc.columbia.edu>; from oleg_inconnu@myrealbox.com on Mon, Aug 26, 2002 at 09:46:40AM -0400

> 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


      reply	other threads:[~2002-08-26 17:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-26 13:46 Oleg
2002-08-26 17:22 ` Xavier Leroy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020826192202.A6666@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=oleg_inconnu@myrealbox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).