HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulebase-4.23.0.0Haskell2010

Foreign.C.String

Utilities for primitive marshalling of C strings.

The marshalling converts each Haskell character, representing a Unicode code point, to one or more bytes in a manner that, by default, is determined by the current locale. As a consequence, no guarantees can be made about the relative length of a Haskell string and its corresponding C string, and therefore all the marshalling routines include memory allocation. The translation between Unicode and the encoding of the current locale may be lossy.

Contents
  1. C strings
  2. Using a locale-dependent encoding
  3. Using 8-bit characters
  4. C wide strings
Synopsis
  • Packagebase-4.23.0.0
  • Exports29
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceString.hs

C strings

typetype CString = Ptr CChar

A C string is a reference to an array of C characters terminated by NUL.

typetype CStringLen = (Ptr CChar, Int)

A string with explicit length information in bytes instead of a terminating NUL (allowing NUL characters in the middle of the string).

Using a locale-dependent encoding

These functions are different from their CAString counterparts in that they will use an encoding determined by the current locale, rather than always assuming ASCII.

Marshal a Haskell string into a NUL terminated C string.

  • the Haskell string may not contain any NUL characters

  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.

valuewithCString :: String -> (CString -> IO a) -> IO a

Marshal a Haskell string into a NUL terminated C string using temporary storage.

  • the Haskell string may not contain any NUL characters

  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

valuewithCStringLen :: String -> (CStringLen -> IO a) -> IO a

Marshal a Haskell string into a C string (ie, character array) in temporary storage, with explicit length information.

  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

Using 8-bit characters

These variants of the above functions are for use with C libraries that are ignorant of Unicode. These functions should be used with care, as a loss of information can occur.

Marshal a Haskell string into a NUL terminated C string.

  • the Haskell string may not contain any NUL characters

  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.

valuewithCAString :: String -> (CString -> IO a) -> IO a

Marshal a Haskell string into a NUL terminated C string using temporary storage.

  • the Haskell string may not contain any NUL characters

  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

valuewithCAStringLen :: String -> (CStringLen -> IO a) -> IO a

Marshal a Haskell string into a C string (ie, character array) in temporary storage, with explicit length information.

  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

C wide strings

These variants of the above functions are for use with C libraries that encode Unicode using the C wchar_t type in a system-dependent way. The only encodings supported are

  • UTF-32 (the C compiler defines STDC_ISO_10646), or

  • UTF-16 (as used on Windows systems).

typetype CWString = Ptr CWchar

A C wide string is a reference to an array of C wide characters terminated by NUL.

typetype CWStringLen = (Ptr CWchar, Int)

A wide character string with explicit length information in CWchars instead of a terminating NUL (allowing NUL characters in the middle of the string).

Marshal a Haskell string into a NUL terminated C wide string.

  • the Haskell string may not contain any NUL characters

  • new storage is allocated for the C wide string and must be explicitly freed using free or finalizerFree.

valuewithCWString :: String -> (CWString -> IO a) -> IO a

Marshal a Haskell string into a NUL terminated C wide string using temporary storage.

  • the Haskell string may not contain any NUL characters

  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

valuewithCWStringLen :: String -> (CWStringLen -> IO a) -> IO a

Marshal a Haskell string into a C wide string (i.e. wide character array) in temporary storage, with explicit length information.

  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.