gpt4 book ai didi

python - 为什么这种 PyArray_SimpleNewFromData 段错误的简单用法?

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:33 26 4
gpt4 key购买 nike

我正在尝试在 python 的 C 扩展中使用 NumPy。

我遇到了无法解释的段错误,我已将其缩小到这个简单的示例。

#include "numpy/arrayobject.h"

int main()
{
int dims[] = {1};
double d[] = {1};

PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, &d);
}

最佳答案

https://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html

This function must be declared so that it is visible to code outside of the routine. Besides adding the methods and constants you desire, this subroutine must also contain calls like import_array() and/or import_ufunc() depending on which C-API is needed. Forgetting to place these commands will show itself as an ugly segmentation fault (crash) as soon as any C-API subroutine is actually called.

我还必须事先包含 Py_Initialize(),因为这是一个独立示例,没有像通常那样通过 Python 虚拟机运行。

关于python - 为什么这种 PyArray_SimpleNewFromData 段错误的简单用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55887324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com