OSError:异常:访问冲突读取 0x0000000000000000。在 python 中使用 .dll 但在调用我的 c 函数时出现错误

问题描述 投票:0回答:0

我正在尝试在 python 脚本中实现 c 函数“main”。调用函数时收到内存访问冲突。我知道我的变量类型匹配并且没有数组边界问题,所以我不确定修复方法是什么。 C函数

float main(int** data) {
    float pi = 3.14159265;
    int max_distance = 182;
    int thetas = 181;
    int accumulator[182][181] = {0};
    int i, j, k, r, m, theta1, radius1;
    int current_max = 0; // initialize current_max to the smallest integer value
    double angle;
    float slope1;
    float d;
    // For loops for iterating through the test array indices, a conditional to check for "hits", followed by a for loop for creating sinusoidal line
    for (i = 0; i < ROWS; i++) {
        k = 127 - i; // Numpy array vs cartesian coordinate (0,0) in numpy array ==> (0,y_max) in cartesian
        for (j = 0; j < COLS; j++) {
            if (i >= 0 && i < ROWS && j >= 0 && j < COLS && data[k][j] != 0) {
                for (m = 0; m < thetas; m++) {
                    angle = (double)(-(pi/2) + (m * pi/2) / 90); // calculating thetas for -pi/2 to pi/2
                    r = fabs((j) * (cos(angle)) + (k) * (sin(angle)) );
                    accumulator[(int) r][m]++;
                }

            }   

        }
            
    }
    // Iterating through every accumulator index, to get
    for (i = 0; i < max_distance; i++) {
        for (j = 0; j < thetas; j++) {
            if (accumulator[i][j] > current_max) {
                current_max = accumulator[i][j];
                radius1 = i;
                theta1 = j-90;
            }
        }
    }

    // Calculating the slope from top radius and theta
    angle = (double)(theta1 * pi / 180); // Converts to radians
    slope1 = (float)(tan(angle + (double)pi/2)); // Make the calculation
    return slope1;
}

Python脚本

import ctypes
from numpy.ctypeslib import ndpointer

# Declare the function signature (return type and argument types)
fun = ctypes.CDLL("C:/Coding_Projects/Hough_Proj/C/HoughTransform.dll")
fun.main.argtypes = [ndpointer(dtype=np.int32, ndim=2)]
fun.main.restype = ctypes.c_float
i = 0
while i < len(slopes):
    j = 0
    currentslopes = slopes[i]
    currenttest = tests[i]
    while j < 47:
        currentset = np.array(currenttest[j], dtype=np.int32)
        result = fun.main(currentset)
        currentslopes.append(result)
        j += 1
    i += 1
python c memory ctypes
© www.soinside.com 2019 - 2024. All rights reserved.