使用NumPy的和numpy.i
,这是很容易的
接口头
#pragma once
void fun(int** outArray, int* nRows, int* nCols);
实施
#include "test.h"
#include <malloc.h>
void fun(int** outArray, int* nRows, int* nCols) {
int _nRows = 100;
int _nCols = 150;
int* _outArray = (int*)malloc(sizeof(int)*_nRows*_nCols);
*outArray = _outArray;
*nRows = _nRows;
*nCols = _nCols;
}
SWIG接口头
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "test.h"
%}
%include "numpy.i"
%init
%{
import_array();
%}
%apply (int** ARGOUTVIEWM_ARRAY2, int* DIM1, int* DIM2) {(int** outArray, int* nRows, int* nCols)}
%include "test.h"
的吨ypemap ARGOUTVIEWM_ARRAY2
创建一个托管的NumPy数组,当NumPy对象在Python中销毁时,自动调用free。
如果你想创建使用Python C API自己的包装,你可以考虑使用numpy.i
看起来像一个重复的SWIG作出生成的代码。这个答案呢? http://stackoverflow.com/questions/22425921/pass-a-2d-numpy-array-to-c-using-ctypes –
这个问题似乎是这个问题的双重性,@TheQuantumPhysicist(numpy - > int ** ,而不是int ** - > numpy) – Eric
int **是动态分配的吗? “删除[]”的工作是谁的? – Eric