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

  • Home
  • SEARCH
  • 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 6093871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:36:40+00:00 2026-05-23T12:36:40+00:00

I am making a simple wrapper for a c library that needs to have

  • 0

I am making a simple wrapper for a c library that needs to have a list of vectors passed to it. It takes an array of pointers to arrays. To make a nice interface I’d like to have Vector (or list) of Vectors, but I can’t really find out how to do this in idiomatic haskell. (Or any other way than memcopying stuff around).

What I’m looking for is something like

Vector (Vector Foo) -> (Ptr (Ptr Foo) -> IO a) -> IO a  
  • 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-23T12:36:40+00:00Added an answer on May 23, 2026 at 12:36 pm

    Edit: hCsound doesn’t deal with this exact case, so I’ve added a full example below.

    You might want to look at my package hCsound (darcs repo), which has to deal with a very similar case.

    Note that it’s very important that the C library doesn’t modify the arrays used by a Data.Vector.Storable.Vector. If you do need to modify the data, you should copy the old data first, modify the array through the ffi, and finally wrap the pointers into a new Vector.

    Here’s the code. As was pointed out in a comment, Data.Vector.Storable.Vector doesn’t have a Storable instance itself, so you’ll need the outer vector to be a Data.Vector.Vector.

    import Foreign.Storable
    import Foreign.Ptr
    import Foreign.ForeignPtr
    import Foreign.Marshal.Array
    
    import qualified Data.Vector as V
    import qualified Data.Vector.Storable as S
    import Data.Vector.Storable.Internal
    
    withPtrArray v f = do
      let vs = V.map S.unsafeToForeignPtr v   -- (ForeignPtr, Offset, Length)
      ptrV = V.toList $ V.map (\(fp,off,_) -> offsetToPtr fp off) vs
      res <- withArray ptrV f
      V.mapM_ (\(fp,_,_) -> touchForeignPtr fp) vs
      return res
    

    Note the array is allocated by withArray, so it’s automatically gc’d after the function returns.

    These arrays aren’t null-terminated, so you’ll need to make sure that the length is passed to the C function by some other method.

    withForeignPtr isn’t used. Instead, touchForeignPtr is called to ensure that the ForeignPtr’s aren’t deallocated before the C function is finished. In order to use withForeignPtr, you’d need to nest calls for each internal vector. That’s what the nest function in the hCsound code does. It’s rather more complicated than just calling touchForeignPtr.

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

Sidebar

Related Questions

I'm making a simple program that maintains a list of numbers, and I want
If I'm making a simple grid based game, for example, I might have a
I'm making a simple tool that will get a string of MySQL commands and
I'm making a simple remove link with an onClick event that brings up a
I have a simple question, but I'm about 80% sure that the answer to
I'm making a simple JavaScript graphing library using the canvas element. I really suck
I'm making a simple class that uses operator<< . It will store two parallel
Hey so i'm making a simple text game using the pdCurses library and a
I'm learning winforms and I have set myself a simple goal of making a
I'm making a free Mac app that is simply a wrapper over the purge

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.