找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1884|回复: 1

[求助] C++返回array,python接收数据不对

1

主题

1

帖子

1

积分

贫民

积分
1
zoe6553 发表于 2019-1-23 13:38:11 | 显示全部楼层 |阅读模式
我写了一个小的C++扩展库:
// TestNumpy.cpp : 定义 DLL 应用程序的导出函数。
//

#include <iostream>
#include <Python.h>
#include <arrayobject.h>

using namespace std;

PyMODINIT_FUNC init_numpy()
{
        import_array();
}

static PyObject *GetArray(PyObject* Self, PyObject* Argvs)
{
        int CArrays[3][3] = { {1, 2, 5}, {4, 7, 8}, {1, 0, 8} };
       
        npy_intp Dims[2] = { 3,3 };
        PyArrayObject *PyArray = (PyArrayObject *)PyArray_SimpleNewFromData(2, Dims, NPY_INT, CArrays);

        return PyArray_Return(PyArray);
}

static PyMethodDef ModulesMethods[] =
{
        {"GetArray", GetArray, METH_VARARGS, "Get array from c to python"},
        {NULL, NULL, 0, NULL}
};

static struct PyModuleDef ModuleDesc =
{
                 PyModuleDef_HEAD_INIT,
                 "TestNumpy_Module",
                 "This module is created by C++. Get array from c to python!",
                 -1,
                 ModulesMethods
};

PyMODINIT_FUNC PyInit_TestNumpy(void)
{
        init_numpy();
        return PyModule_Create(&ModuleDesc);
}


Python 代码:
import TestNumpy as tn

tmp = tn.GetArray()
print(tmp)


打印结果是:
array([[1852914064,        701, 1845666960],
       [         0,          0,          0],
       [1852914064,        701, 1867854904]], dtype=int32)


这是什么情况,而且打印值会一直变。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表