我正在尝试使用ctypes从python向C ++库传递一个双int数组。不幸的是,当我尝试读取C ++端的数组条目时,我得到了一个段错误。这样做的正确方法是什么?
这是我的python代码:
#! /usr/bin/env python
from ctypes import *
# load the extension lib
testLib = cdll.LoadLibrary('./libtest.so')
# build adjacency matrix
numNodes = 5
MatrixRowType = c_int * numNodes
MatrixType = MatrixRowType * numNodes
adj = MatrixType()
for i in range(0, numNodes):
for j in range(0, numNodes):
adj[i][j] = c_int(0)
adj[0][0] = c_int(42)
for i in range(numNodes):
print ' matrix value ', adj[i][0]
testLib.test_bridge(numNodes, adj)
这是我的C ++代码:
#include <stdlib.h>
#include <iostream>
extern "C" {
int test_bridge(int numNodes, int** adjacencyMatrix) {
std::cout << " numNodes:" << numNodes << '\n';
std::cout << " passed adjacency pointer " << adjacencyMatrix << '\n';
std::cout << adjacencyMatrix[0][0] << std::endl; // should be "42", instead we crash
}
}