Segmentation fault when using weave.inline
I am new to embedding C ++ code in Python. I am testing weave.inline. However, when my code is executed, a segmentation fault occurs. Can anyone 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')
+3
source to share
1 answer
I see two questions here:
1) indention - your python function should indented from the line def
2) your argument weave.inline()
must be a list. For details see.
So the corrected code should look like this:
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 works for me.
0
source to share