使用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