简体   繁体   English

为什么地址清理程序会在我的乘法矩阵中导致堆缓冲区溢出错误?

[英]Why is address-sanitizer causing a heap-buffer-overflow error in my multiplication matrix?

The error is: heap-buffer-overflow .错误是:堆缓冲区溢出 I'm running a block of code that multiplies matrices.我正在运行一个将矩阵相乘的代码块。 Address sanitizer is throwing me an error at a specific line while trying to multiply two matrices.地址清理程序在尝试将两个矩阵相乘时在特定行向我抛出错误。 On my IDE, no errors or warnings show up, however, address sanitizer is throwing an error here and I'm not too sure why.在我的 IDE 上,没有出现错误或警告,但是,地址清理程序在这里抛出错误,我不太清楚为什么。 The matrix has entries scanned in from the user, below is a snippet of the code not working.该矩阵具有从用户扫描的条目,下面是代码片段不起作用。 The snippet address sanitizer is throwing an error on is bolded.代码段地址清理程序抛出错误以粗体显示。 Thanks.谢谢。

snippet:片段:

double **productMatrixT;

productMatrixT = (double **)malloc(rowT*sizeof(double));
for(i = 0; i < rowT; i++)
{
  productMatrixT[i] = malloc(column*sizeof(double));
}

double sum = 0;
for(i = 0; i < column; i++)
{
  for(j = 0; j < row; j++)
   {
     for(k = 0; k < rowT; k++)
      {
        **sum = sum + matrixT[i][k] * matrix[k][j];** <---- /*says this line is a cause for a problem*/
      }
       productMatrixT[i][j] = sum;
       sum = 0;
     }
  }
}

free:自由的:

for(i = 0; i < rowT; i++)
{
 free(productMatrixT[i]);
}
free(productMatrixT);

regarding:关于:

double sum = 0;

since it is declared as a double , it should be initialized as a double , IE由于它被声明为double ,因此应将其初始化为double ,即 IE

double sum = 0.0;

regarding:关于:

**sum = sum + matrixT[i][k] * matrix[k][j];

since 'sum' is a double and not a pointer to a pointer, the ** dereferencing is resulting in some random address.由于“和”是double而不是指向指针的指针,因此**取消引用会产生一些随机地址。 This is what the address sanitizer is complaining about.这就是address sanitizer程序所抱怨的。

As others have mentioned, there is plenty more wrong with the posted code.正如其他人所提到的,发布的代码还有很多错误。

Your indexing is wrong, in你的索引是错误的,在

sum = sum + matrixT[i][k] * matrix[k][j];

you access matrix as column x rowT and rowT x row but you've allocated it as rowT x column .您将matrix作为column x rowTrowT x row访问,但您已将其分配为rowT x column

Also you should fix你也应该修复

productMatrixT = (double **)malloc(rowT*sizeof(double));

to be成为

productMatrixT = (double **)malloc(rowT*sizeof(double *));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM