Python 来自c++; 我需要修改我从Cython传递到C++函数的NUMPY数组。一切都很好,但是当我在调用C++修饰符函数之后打印出这个值时,这个值和调用函数之前的值保持不变。我也用string做了同样的尝试,但效果不太好。我使用键入的MemoryView来访问内存。下面是我使用的代码(只保留与此问题相关的内容)

Python 来自c++; 我需要修改我从Cython传递到C++函数的NUMPY数组。一切都很好,但是当我在调用C++修饰符函数之后打印出这个值时,这个值和调用函数之前的值保持不变。我也用string做了同样的尝试,但效果不太好。我使用键入的MemoryView来访问内存。下面是我使用的代码(只保留与此问题相关的内容),python,c++,numpy,pointers,cython,Python,C++,Numpy,Pointers,Cython,测试.h #include <iostream> struct S1 { float* buffer; int length; }; int modify(S1* s1, char* str); **setup.py** from setuptools import setup from distutils.extension import Extension from Cython.Build import cythonize import numpy e

测试.h

#include <iostream>

struct S1 {
    float* buffer;
    int length;
};

int modify(S1* s1, char* str);
**setup.py**

from setuptools import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy


extensions = [
    Extension("temp",
              sources=["test.pyx", "test_modify.cpp"],
              include_dirs=[numpy.get_include()],
              extra_compile_args=["-O3", '-std=c++11'],
              language="c++")
]

setup(
    ext_modules=cythonize(extensions)
)

# to install run, python setup.py build_ext --inplace
test.py(构建后运行)

:

默认情况下,
astype
始终返回新分配的数组

i、 e.
d_视图
d
副本的视图,而不是
d
的视图。因此,我们不希望
d_视图所做的更改反映在
d


这是一个基本的C指针传递问题str
s
都指向同一个位置,但它们是不同的指针。在
str=“newstr”
中重新分配
str
后,它们现在指向不同的位置

您可能想要一个指向一个指针(
char**
)的指针

:

默认情况下,
astype
始终返回新分配的数组

i、 e.
d_视图
d
副本的视图,而不是
d
的视图。因此,我们不希望
d_视图所做的更改反映在
d


这是一个基本的C指针传递问题str
s
都指向同一个位置,但它们是不同的指针。在
str=“newstr”
中重新分配
str
后,它们现在指向不同的位置


您可能需要一个指向指针(
char**
)?

test.pyx中的
depthBuffer
是什么?它没有定义。您的代码没有生成,请提供。为了值得,我希望更改
S1
中的数组以使其工作(除了我们不知道
w
h
),但不知道字符串。我已经用一个最小的可复制示例更新了代码,该示例应该生成。我使用Cython0.29.21版本进行构建。THNX打印d和s时,输出是什么?test.pyx中的
depthBuffer
是什么?它没有定义。您的代码没有生成,请提供。为了值得,我希望更改
S1
中的数组以使其工作(除了我们不知道
w
h
),但不知道字符串。我已经用一个最小的可复制示例更新了代码,该示例应该生成。我使用Cython0.29.21版本进行构建。Thnx当您打印d和s时,输出是什么?它按预期工作。谢谢我不知道astype返回一个新分配的数组。Aso,我用C++已经多年了,所以忘记了大部分的概念。谢希特工作如期。谢谢我不知道astype返回一个新分配的数组。Aso,我用C++已经多年了,所以忘记了大部分的概念。谢谢
from numpy import pi, cos, sin, arccos, arange
import numpy as np
cimport numpy as np

np.import_array()
cdef extern from "test_modify.h":

    cdef struct S1:
        float* buffer
        int length

    int modify(S1* s1, char* str)

def modifyPY():
    d = np.zeros((2, 3, 3), dtype=np.float32)
    cdef float[:, :, ::1] d_view = d.astype(np.float32)
    cdef S1 s1 = [&(d_view[0, 0, 0]), np.product(d.shape)]

    cdef char *s = 'jhk'

    modify(&s1, s)

    return d, s
from setuptools import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy


extensions = [
    Extension("temp",
              sources=["test.pyx", "test_modify.cpp"],
              include_dirs=[numpy.get_include()],
              extra_compile_args=["-O3", '-std=c++11'],
              language="c++")
]

setup(
    ext_modules=cythonize(extensions)
)

# to install run, python setup.py build_ext --inplace
import temp

d, s = temp.modifyPY()

print(d) # still 0's, should be 10's
print(s) # still "jhk" should be "newstr'
cdef float[:, :, ::1] d_view = d.astype(np.float32)
int modify(S1* s1, char* str) {
    str = "newstr"; // string modify