[英]Segmentation fault when using weave.inline
I am new to embedding C++ code into Python. 我是将C ++代码嵌入Python的新手。 I am testing weave.inline. 我正在测试weave.inline。 However I get a segmentation fault when running my code. 但是,在运行代码时出现分段错误。 Could someone tell me what I am doing wrong? 有人能告诉我我做错了什么吗? Here is my code: 这是我的代码:
from scipy import weave
def cpp_call(np_points):
assert(type(np_points) == type(1))
code = """
double z[np_points+1][np_points+1];
for (int x = 0; x <= np_points; x++)
{
for (int y = 0; y <= np_points; y++)
{
z[x][y] = x*y;
}
}
"""
return weave.inline(code,'np_points')
I see two issues here: 我在这里看到两个问题:
1) indention - your python function needs to be indented past the def
line 1)缩进 - 你的python函数需要缩进越过def
行
2) your argument to the weave.inline()
should be a list. 2)你对weave.inline()
)的参数应该是一个列表。 See here for details 详情请见此处
So the corrected code should look like: 因此,更正的代码应如下所示:
from scipy import weave
def cpp_call(np_points):
assert(type(np_points) == type(1))
code = """
double z[np_points+1][np_points+1];
for (int x = 0; x <= np_points; x++)
{
for (int y = 0; y <= np_points; y++)
{
z[x][y] = x*y;
}
}
"""
return weave.inline(code,['np_points']) #Note the very important change here
This code runs fine for me. 这段代码对我来说很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.