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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:43:29+00:00 2026-05-25T20:43:29+00:00

I have a C++ callback function that calls into Python using ctypes. This function’s

  • 0

I have a C++ callback function that calls into Python using ctypes. This function’s parameters are a pointer to an array of double and the number of elements.

There are a lot of elements, approximately 2,000,000. I need to send this into scipy functions.

The C++ prototype is :

bool (*ptsetDataSource)(double*, long long);

which is the following python code:

CPF_setDataSource = CFUNCTYPE(c_bool, POINTER(c_double),c_longlong)
CPF_setSelection= CFUNCTYPE(c_bool,c_char_p, c_longlong,c_longlong)
CPF_ResetSequence = CFUNCTYPE(c_bool)

def setDataSource(Data, DataLength):
    Datalist=[0.0]*100
    for i in range(0,100):
        Datalist[i]=Data[i]

    print Datalist
    return True

The problem is that print datalist returns:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

which is not correct(data is filled with a lot of other numbers when checked on the c++ side.

Also, if I use this code to convert the data to a python list, it locks up the computer at the allocate step.

Is there anyway to load the data from the C++ array and then convert it to an array fit for scipy?

  • 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-25T20:43:30+00:00Added an answer on May 25, 2026 at 8:43 pm

    If Data were (c_double*DataLength.value) array then you could:

    a = np.frombuffer(Data) # no copy. Changes in `a` are reflected in `Data`
    

    If Data is a POINTER(c_double) you could get numpy array using numpy.fromiter(). It is the same loop as in your question but faster:

    a = np.fromiter(Data, dtype=np.float, count=DataLength.value) # copy
    

    To create a numpy array from POINTER(c_double) instance without copying you could use .from_address() method:

    ArrayType = ctypes.c_double*DataLength.value
    addr = ctypes.addressof(Data.contents)
    a = np.frombuffer(ArrayType.from_address(addr))
    

    Or

    array_pointer = ctypes.cast(Data, ctypes.POINTER(ArrayType))
    a = np.frombuffer(array_pointer.contents)
    

    Both methods convert POINTER(c_double) instance to (c_double*DataLength) before passing it to numpy.frombuffer().

    Cython-based solution

    Is there anyway to load the data from the C++ array and then convert it to an array fit for scipy?

    Here’s C extension module for Python (written in Cython) that provide as C API the conversion function:

    cimport numpy as np
    np.import_array() # initialize C API to call PyArray_SimpleNewFromData
    
    cdef public api tonumpyarray(double* data, long long size) with gil:
        if not (data and size >= 0): raise ValueError
        cdef np.npy_intp dims = size
        #NOTE: it doesn't take ownership of `data`. You must free `data` yourself
        return np.PyArray_SimpleNewFromData(1, &dims, np.NPY_DOUBLE, <void*>data)
    

    It could be used with ctypes as follows:

    from ctypes import (PYFUNCTYPE, py_object, POINTER, c_double, c_longlong,
                        pydll, CFUNCTYPE, c_bool, cdll)
    
    import pointer2ndarray
    tonumpyarray = PYFUNCTYPE(py_object, POINTER(c_double), c_longlong)(
        ("tonumpyarray", pydll.LoadLibrary(pointer2ndarray.__file__)))
    
    @CFUNCTYPE(c_bool, POINTER(c_double), c_longlong)
    def callback(data, size):
        a = tonumpyarray(data, size)
        # call scipy functions on the `a` array here
        return True
    
    cpplib = cdll.LoadLibrary("call_callback.so") # your C++ lib goes here
    cpplib.call_callback(callback)
    

    Where call_callback is: void call_callback(bool (*)(double *, long long)).

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

Sidebar

Related Questions

I have a member function in a class that has a callback, but the
I have an array of function callbacks, like this: class Blah { private var
Let's say you have a Javascript function that calls a web service method. So
If i have a function like this function do(callback) { //do stuff callback(); }
I have a RIA Services data service that has several function calls that look
I have a list of callback functions that I need to invoke when an
Eg. I have following delegate method I want to use as a callback function
I have a function function callback(obj){...} Is it okay to pass in more objects
I have a javascript/jQuery block as a callback after $.get function: function myCallBack(data, textStatus)
I have a js function which has, until now, always been the callback for

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.