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 8345921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:50:33+00:00 2026-06-09T06:50:33+00:00

How do you wrap a C function call that expects a pre-allocated char** as

  • 0

How do you wrap a C function call that expects a pre-allocated char** as an argument to store the result? I am trying to return a python list result.

I have found examples of the other way around, and also this ctypes example, but I am not entirely sure ctypes is the appropriate approach in cython.

For reference, I am practicing wrapping the openni library:
http://openni.org/Documentation/Reference/classxn_1_1_pose_detection_capability.html

The original C signature I am wrapping is (its actually a C++ method that just wraps around a C function internally):

/**
 * @brief Gets the names of all poses supported by this capability.

 * @param [out]     pstrPoses   Pre-allocated memory for the names of the supported poses.
 * @param [in,out]  nPoses      In input - size of the preallocated memory, in output
 *                              - the number of pose names.
 */

XnStatus GetAvailablePoses(XnChar** pstrPoses, XnUInt32& nPoses) const

(XnChar is just a typedef for char)

Here is my attempt so far, which crashes:

from libc.stdlib cimport malloc, free

def get_available_poses(self):
    cdef: 
        int i 
        bytes name 
        XnStatus stat
        XnUInt32 size = self.handle.GetNumberOfPoses()
        XnChar **buf = <XnChar**>malloc(size * sizeof(XnChar*))

    if not buf:
        raise MemoryError()

    try:
        # this crashes: Segmentation fault
        stat = self.handle.GetAvailablePoses(buf, size)

        # if I could get to here, I would want to 
        # build a list to return (not saying this is
            # even correct either)
        for i in range(size):
            name = <char*>(buf[i])
            ...

    finally:
        free(buf)

That version of the C function is technically deprecated, but the newer one looks even scarier to me:

/**
 * Gets the names of all poses supported by this capability.

 * @param [out]     pstrPoses       Pre-allocated memory for the names of the supported poses.
 * @param [in]      nNameLength     Memory size for each pose name.
 * @param [in,out]  nPoses          In input - size of the preallocated memory, in output
 *                                  - the number of pose names.
 */

 XnStatus GetAllAvailablePoses(XnChar** pstrPoses, XnUInt32 nNameLength, 
                                XnUInt32& nPoses) const;

Ideally if I could figure out how to pass in a proper char** and produce a list, I would use the newer one that also requires me to specify the length of the names that was allocated.

Update:

I simplified this problem down to just the basics to make sure I am doing this right in the first place:

src:

//chars.h

void setChars(char** str_array, int size);

//chars.cc

#include "chars.h"

void setChars(char** str_array, int size) {
    for (int i = 0; i < size; i++) {
        char *s = "FOO";
        str_array[i] = s;
    }
}

cython:

#chars.pxd

cdef extern from "chars.h":
    void setChars(char**, int)

#chars.pyx

from libc.stdlib cimport malloc, free

def py_setChars():
    cdef: 
        bytes s
        int i
        int size = 6
        char** buf = <char**>malloc(size * sizeof(char*))

    if not buf:
        raise MemoryError()

    out = []

    try:
        setChars(buf, size)

        for i in range(size):
            s = buf[i]
            out.append(s)

    finally:
        free(buf)

    return out

And it works as expected:

In [1]: import chars
In [2]: chars.py_setChars()
Out[2]: ['FOO', 'FOO', 'FOO', 'FOO', 'FOO', 'FOO']

I am guessing the GetAllAvailablePoses() call I want to use is expecting some kind of pre-allocated memory that I am not doing right, hence the param asking for the size of each char.

  • 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-06-09T06:50:36+00:00Added an answer on June 9, 2026 at 6:50 am

    I finally figured this out with enough playing. GetAllAvailablePoses really does expect you to preallocate the space of each char* and tell it how big that space is for it to populate:

    # foo.pxd
    
    XnStatus GetAllAvailablePoses(XnChar **, XnUInt32, XnUInt32&)
    
    # foo.pyx
    
    def get_available_poses(self):
        cdef: 
            int i 
            bytes name 
            XnStatus stat
            XnUInt32 nameLength = 256
            XnUInt32 size = self.handle.GetNumberOfPoses()
    
        cdef XnChar **buf = <XnChar**>malloc(size * sizeof(XnChar*))
    
        if not buf:
            raise MemoryError()
    
        for i in range(size):
            buf[i] = <XnChar*>malloc(nameLength)
    
        out = [None]*size
    
        try:
            stat = self.handle.GetAllAvailablePoses(buf, nameLength, size)
    
            for i in range(size):
                name = <char*>buf[i]
                out[i] = name
    
        finally:
            free(buf)
    
        return out
    
    # Out: ['Psi', 'CrossHandsPose', 'Wave', 'Click', 'RaiseHand', 'MovingHand']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a function that can wrap any other function call.
I am trying to create a function guilabel_wrap(char *buffer, int maxlen) that takes a
You can I wrap this code so that when a call a function returns
I want to wrap every function call with some logging code. Something that would
How can I write a wrapper that can wrap any function and can be
Trying to wrap my head around the jQuery .not() function, and running into a
I need to write a delegate function that can 'wrap' some while/try/catch code around
Trying to hook into the function comment_text() supplied by Wordpress API to wrap the
So I know that you can wrap a function around another function by doing
I am trying to do validation for every function I call in a script

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.