Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:11:26+00:00 2026-05-27T09:11:26+00:00

I am trying to write a storable vector instance for CString (null-terminated C chars

  • 0

I am trying to write a storable vector instance for CString (null-terminated C chars in my case). The storable instance will store the pointers that the CString is (Ptr CChar). So, length of the vector is the number of CString pointers. Now, the reason I am writing this storable instance is because it will be used to do zero-copy from FFI CString, and then fast ByteString build using unsafeCreate (after some transformation – so, we use fast vectors here for intermediate operations). In order to do fast ByteString build, three things are needed for storable instance:

  • Total length in bytes – the storable instance needs to have book-keeeping allocation for storing the length of each CString when adding it to the vector, and total length of CString stored so far. Let us say total length of C Strings can’t exceed 2^31. So, Int32/Word32 will do to store the length of each CString, and total length.
  • Function to store CString and its length – O(n) time. This function will walk the CString, and store its length, and also, increment the total length by the length of the CString.
  • Functon to return length in total bytes – O(1) time. This function will just retrieve value from the field that stores the total length

While I know how to write custom storable instance, I don’t know how to handle this kind of case. A simple code (can be a simple toy example) that shows how to do custom bookkeeping, and write function to store/get bookkeeping results will be very much appreciated.

Update 1 (Clarification)

The reason for using storable vector instance in my case is two-fold: fast computation/transformation using unboxed types (on real-time data received through C FFI), and fast conversion to bytestring (to send out the data in real-time over IPC to another program). For fast bytestring conversion, unsafeCreate is excellent. But, we must know how much to allocate, and also, pass it a function for transformation. Given a storable vector instance (with mixed types – I simplified my question above to just CString type), it is easy for me to build a fast transform function that walks each element of the vector and transforms it to bytestring. Then, we simply pass it to unsafeCreate. But, we must also pass it number of bytes to allocate. A O(n) recursive byte length calculation function is too slow, and can double the overhead of building bytestring.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:11:27+00:00Added an answer on May 27, 2026 at 9:11 am

    It sounds like you want to write something like this. Note that this code is not tested.

    -- The basic type.  Export the type but not the constructors or 
    -- accessors from the module.
    data StringVector {
       strVecLength :: Word32,               -- Total length
       strVecContents [(Word32, Ptr CChar)]  -- (Length, value) pairs
    }
    
    -- Invariants: forall (StringVector len contents), 
    --    len == sum (map fst) contents
    --    all (\p -> fst p == c_strlen (snd p)) contents
    
    
    -- The null case.
    emptyStrVec :: StringVector
    emptyStrVec = StringVector 0 []
    
    
    -- Put a new Cstring at the head of the vector.  Analogous to ":".
    stringVectorCons :: Ptr CChar -> StringVector -> StringVector
    stringVectorCons ptr (StringVector len pairs) = 
       StringVector (len + n) $ (n, ptr) : pairs
       where
          n = c_strlen ptr   -- Or whatever the right function name is
    
    
    -- Extract the head of the vector and the remaining vector.
    stringVectorUncons :: StringVector -> ((Word32, Ptr CChar), StringVector)
    stringVectorUncons (StringVector len (h:t)) =
       (h, StringVector (len - fst h) t)
    

    After that you can add whatever other functions you might want, depending on the application. Just make sure that each function preserves the invariants.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file
im trying to write an app that will display a list off lines from
I'm trying to write a regex function that will identify and replace a single
I am trying write a query that will display if the person has anyone
Trying to write a regular expression for java that will return true ONLY if
I'm trying to write a custom WPF ValidationRule to enforce that a certain property
Trying to write a code that searches hash values for specific string's (input by
Trying to write a game such that most of the screen gets filled with
Im trying to write a simple script that slides the next 'slide' div in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.