为什么这个C代码产生双重免费或损坏?

时间:2016-01-10 01:41:47

标签: c memory-corruption double-free

为什么这个用于计算两个向量的内积的代码在编译时会产生双重释放或损坏错误:

ejspeiro@Eduardo-Alienware-14:~/Dropbox/HPC-Practices$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4

代码来自this reference

// Computation of the inner product of vectors aa and bb.

#include <stdio.h>
#include <stdlib.h>

int main() {

  size_t nn = 100000000;
  size_t total_mem_array = nn*sizeof(double);

  double *aa;
  double *bb;
  double ss = 0.0;

  aa = (double *) malloc(total_mem_array);
  bb = (double *) malloc(total_mem_array);

  int ii = 0;

  for (ii = 0; ii < nn; ++ii) {
    aa[ii] = 1.0;
    bb[ii] = 1.0;
  }

  double sum1 = 0.0;
  double sum2 = 0.0;

  for (ii = 0; ii < nn/2 - 1; ++ii) {
    sum1 += (*(aa + 0))*(*(bb + 0));
    sum2 += (*(aa + 1))*(*(bb + 1));
    aa += 2;
    bb += 2;
  }
  ss = sum1 + sum2;

  free(aa);
  free(bb);

  return 0;
}

2 个答案:

答案 0 :(得分:6)

导致错误是因为传递给free()的值与malloc()返回的值不同,因为您增加了aabb

要纠正它,您可以,例如,定义两个仅用于内存管理的附加指针变量,即分配和释放。一旦获取了内存,请将其分配给aabb

答案 1 :(得分:0)

您可以简化:

for (ii = 0; ii < nn/2 - 1; ++ii) {
    sum1 += (*(aa + 0))*(*(bb + 0));
    sum2 += (*(aa + 1))*(*(bb + 1));
    aa += 2;
    bb += 2;
}

为:

for (ii = 0; ii < nn/2 - 1; ++ii) {
    sum1 += aa[ii * 2]     * bb[ii * 2];
    sum2 += aa[ii * 2 + 1] * bb[ii * 2 + 1];
}

它具有避免增加指针导致问题的双重好处,并使代码更加清晰。