为什么此 C 代码会产生双重释放或损坏?
Why does this C code yields a double free or corruption?
为什么这个用于计算两个向量内积的代码在编译时会产生双重自由或损坏错误:
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;
}
错误是因为传递给 free()
的值与 malloc()
返回的值不同,因为您递增 aa
和 bb
.
要更正它,例如,您可以定义两个额外的指针变量,它们仅用于内存管理,即分配和释放。一旦他们获得内存,将其分配给 aa
和 bb
.
您可以简化:
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];
}
这有双重好处:避免增加导致问题的指针,并使代码更清晰。
为什么这个用于计算两个向量内积的代码在编译时会产生双重自由或损坏错误:
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;
}
错误是因为传递给 free()
的值与 malloc()
返回的值不同,因为您递增 aa
和 bb
.
要更正它,例如,您可以定义两个额外的指针变量,它们仅用于内存管理,即分配和释放。一旦他们获得内存,将其分配给 aa
和 bb
.
您可以简化:
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];
}
这有双重好处:避免增加导致问题的指针,并使代码更清晰。