[英]Segmentation fault when using weave.inline
我是将C ++代码嵌入Python的新手。 我正在测试weave.inline。 但是,在运行代码时出现分段错误。 有人能告诉我我做错了什么吗? 这是我的代码:
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')
我在这里看到两个问题:
1)缩进 - 你的python函数需要缩进越过def
行
2)你对weave.inline()
)的参数应该是一个列表。 详情请见此处
因此,更正的代码应如下所示:
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
这段代码对我来说很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.