Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用gcc/ubuntu的if子句中的分段错误_Python_C_Gcc - Fatal编程技术网

Python 使用gcc/ubuntu的if子句中的分段错误

Python 使用gcc/ubuntu的if子句中的分段错误,python,c,gcc,Python,C,Gcc,我正在编写一个用于python的c函数。运行时,出现分段错误,根据printf调用,该错误被抛出if子句。shell的输出为: row 1, col 0: 1.000000 row:0, -col:0, index:0 -2: 0.000000 else row:0, -col:1, index:1 -2: 0.000000 else row:0, -col:2, index:2 -2: 0.000000 else row:0, -col:3, index:3 -2: 0.000000 else

我正在编写一个用于python的c函数。运行时,出现分段错误,根据printf调用,该错误被抛出if子句。shell的输出为:

row 1, col 0: 1.000000
row:0, -col:0, index:0
-2: 0.000000
else
row:0, -col:1, index:1
-2: 0.000000
else
row:0, -col:2, index:2
-2: 0.000000
else
row:0, -col:3, index:3
-2: 0.000000
else
row:0, -col:4, index:4
-2: 0.000000
else
row:1, -col:0, index:5
-2: 1.000000
Speicherzugriffsfehler (Speicherabzug geschrieben)
(最后一行表示分段故障)

c代码是:

#include <stdio.h>
#include <math.h>

#define pi 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

void hough(const void* img, int imgRowCount, int imgColCount, const void* thetas, int thetaCount, const void* rhos, int rhoCount, void* out)
{

  const double* imgD = (double*) img;
  double* outD = (double*)out;

  const double* thetasD = (double*)thetas;
  const double* rhosD = (double*)rhos;



  printf("row 1, col 0: %f\n", imgD[getIndex(1, 0, imgColCount)]);


  int row, col, thetaInd, rhoInd, index;
  double rhoVal, minDiff, diff, tmp;
  for(row = 0; row<imgRowCount; row++)
  {
      for(col = 0; col<imgColCount; col++)
      {
          printf("row:%d, -col:%d, index:%d\n", row, col, getIndex(row, col, imgColCount));
          tmp = imgD[getIndex(row, col, imgColCount)];
          printf("-2: %f\n", tmp);
          if (tmp>0.0)
          {
              printf("-1");
              for(thetaInd = 0; thetaInd<thetaCount; thetaInd++)
              {
                  rhoVal = col*cos(thetasD[thetaInd]*(pi/180)) + row*sin(thetasD[thetaInd]*(pi/180));
                  minDiff = INFINITY;
                  index = -1;
                  for(rhoInd = 0; rhoInd<rhoCount; rhoInd++)
                  {
                      diff = abs(rhoVal-rhosD[rhoInd]);
                      if(diff<minDiff)
                      {
                          minDiff = diff;
                          index = rhoInd;
                      }
                  }
                  if(index>=0)
                  {
                    printf("1\n");
                    outD[getIndex(index, thetaInd, thetaCount)] += 1;
                  }
              }
          }
          else
          {
            printf("else\n");
          }
      }

  }


}



int getIndex(int row, int col, int maxCol)
{
    return col + row*maxCol;
}
我做错了什么?
谢谢

唯一可能导致该错误的原因是数组超出范围。在
[]
内部使用函数
getIndex(…)
可能会导致您的问题


但是,由于难以阅读代码(没有注释,也没有上下文),我建议使用调试器(如
valgrind
)为您提供有关错误位置的信息。事实上,
valgrind
甚至会打印出现错误的行号,前提是您使用调试符号(
-g-O0
在gcc和clang上)进行编译。

从输出来看,错误似乎发生在这部分代码中:

          for(thetaInd = 0; thetaInd<thetaCount; thetaInd++)
          {
              rhoVal = col*cos(thetasD[thetaInd]*(pi/180)) + row*sin(thetasD[thetaInd]*(pi/180));
              minDiff = INFINITY;
              index = -1;
              for(rhoInd = 0; rhoInd<rhoCount; rhoInd++)
              {
                  diff = abs(rhoVal-rhosD[rhoInd]);
                  if(diff<minDiff)
                  {
                      minDiff = diff;
                      index = rhoInd;
                  }
              }

              if(index>=0)
              {
                printf("1\n");
                outD[getIndex(index, thetaInd, thetaCount)] += 1;
              }
          }
应该是这样的:

 lib.hough(imgC.ctypes.data_as(ctypes.c_void_p), imgC.shape[0], imgC.shape[1],
   thetasC.ctypes.data_as(ctypes.c_void_p), len(thetasC), 
   rhosC.ctypes.data_as(ctypes.c_void_p), len(rhosC), 
   outC.ctypes.data_as(ctypes.c_void_p))

在代码中,它到底在哪里崩溃?在行中添加注释。考虑在每一个代码> Prtff<代码>之后添加<代码> FFLUW(STDUT),以获得执行到哪里的准确信息。如果使用bash,请使用
ulimit-c unlimited
。运行程序,然后使用核心转储和调试器找到失败的地方。所有这些强制转换都不是必需的,只需添加杂波即可。检查
getIndex
中的返回值。如果在
[]
中的多个位置嵌套调用,则可能会超出数组边界。谢谢,多大的错误啊-有可能在c代码中检查这样的错误并给出相关的预期吗?静态分析器,比如or
gcc-Wall-ansi-pedantic
,可能也会有帮助。我的意思是在c方法的开头写几行代码来检查,不管是否给出了所需的参数。e、 g:if(img未定义)引发异常
lib.hough(imgC.ctypes.data_as(ctypes.c_void_p), imgC.shape[0], imgC.shape[1], 
  thetasC.ctypes.data_as(ctypes.c_void_p), len(thetasC), 
  rhosC.ctypes.data_as(ctypes.c_void_p), 
  outC.ctypes.data_as(ctypes.c_void_p))
 lib.hough(imgC.ctypes.data_as(ctypes.c_void_p), imgC.shape[0], imgC.shape[1],
   thetasC.ctypes.data_as(ctypes.c_void_p), len(thetasC), 
   rhosC.ctypes.data_as(ctypes.c_void_p), len(rhosC), 
   outC.ctypes.data_as(ctypes.c_void_p))