[英]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.