Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamically allocated array
#1
Hello,

Well, I'm not sure if I can ask this question here.. Please let me know if I came to wrong place.

I want to wrap c++ code to make pyd and want to use the function in python code.
I sent numpy array data to c++ code and return array from c++ code and received it in python code as a numpy array.
However, if I deleted dynamically allocated array in c++ code, the returned array has a garbage like values.

Here is my code, written in c++.

#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "numpy\core\include\numpy\ndarraytypes.h"
#include "numpy\core\include\numpy\ndarrayobject.h"
#include "numpy\core\include\numpy\arrayobject.h"


int test (...)
{
   // Do some job here

    return 0;
}

static PyObject* ctest(PyObject* self, PyObject* args)
{

    Py_Initialize();
    import_array();
    
    PyArrayObject* rawData;
    int lenRawData;
    double* cRawData;
    
    if (!PyArg_ParseTuple(args, "Oi", &rawData, &lenRawData))
    {
        return NULL;
    }
    
    cRawData = (double*)PyArray_DATA(rawData);
    double* processedData = new double[lenRawData]();

    PyObject* arrayProcessedData;
    PyArrayObject* narrayProcessedData;

    test(cRawData, processedData); // I wanted to delete this 'processedData' and passed it as an argument

    npy_intp dims[1]; 
    dims[0] = lenRawData;
    arrayProcessedData = PyArray_SimpleNewFromData(1, dims, NPY_FLOAT64, 
    (void*)processedData);

    narrayProcessedData = reinterpret_cast<PyArrayObject*>(arrayProcessedData);


    delete[] processedData;

    return Py_BuildValue("O", narrayProcessedData);
}

static PyObject* version(PyObject* self)
{
    return Py_BuildValue("s", "Version 0.1");
}

static PyMethodDef myMethods[] = {
    {"test", ctest, METH_VARARGS, "test LPF"},
    {"version", (PyCFunction)version, METH_NOARGS, "return the version."},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef LPFtest = {
    PyModuleDef_HEAD_INIT,
    "LPFtest",
    "test",
    -1,
    myMethods
};

PyMODINIT_FUNC PyInit_LPFtest(void)
{
    return PyModule_Create(&LPFtest);
}
If I delete 'processedData' (line #46) then the returned array has a garbage. If I do not delete 'processedData' then I can get an array with correct values in python code. However, of course, if I don't delete 'processedData' then there is a memory leak.

I think I missed very simple and basic things about pointer.
It seems all the arrays cast from deleted array are deleted or messed up.
Actually I only used python these days and not familiar with pointer. I'm sorry for the stupid question.


Please let me know if you need further information.
Thank you in advance!

Best wishes,
JESuh
Reply
#2
There are several blogs on this subject:
google: 'wrapping c++ in python'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Populating an array dynamically zxcv101 1 1,149 May-17-2022, 11:24 AM
Last Post: deanhystad
  Loop through array items dynamically in a certain format bhojendra 3 2,655 Jun-11-2019, 03:37 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020