分配数组并释放它后,我显示一条错误消息,如下所示:
*** glibc detected *** ./Q3: malloc(): memory corruption (fast): 0x09a092f8 ***
我该如何补救这种情况?其背后的潜在结果可能是什么?
使用的代码如下:
// The compilation command used is given below
// gcc Q3.c nrutil.c DFRIDR.c -lm -o Q3
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "nr.h"
#define LIM1 20.0
#define a -5.0
#define b 5.0
#define pre 100.0 // This defines the pre
/* This file calculates the func at given points, makes a
* plot. It also calculates the maximum and minimum of the func
* at given points and its first and second numerical derivative.
*/
float func(float x)
{
return exp(x / 2) / pow(x, 2);
}
int main(void)
{
FILE *fp = fopen("Q3data.dat", "w+"), *fp2 = fopen("Q3results.dat", "w+");
int i; // Declaring our loop variable
float x, y, min, max, err, nd1, nd2;
// These arrays are defined so that we can pass them into Numerical Recipes routines
float * xp = malloc(((b - a) / pre) * sizeof(float));
float * yp = malloc(((b - a) / pre) * sizeof(float));
float yp1 = dfridr(func, a, 0.1, &err), ypn = dfridr(func, b, 0.1, &err);
// Define the initial value of the func to be the minimum
min = func(0);
for(i = 0; x < LIM1 ; i++)
{
x = i / pre; // There is a singularity at x = 0
y = func(x);
if(y < min)
min = y;
fprintf(fp, "%f t %f n", x, y);
}
fprintf(fp, "nn");
max = 0;
for(i = 0, x = a; x < b; i++)
{
xp[i] = a + i / pre;
yp[i] = func(xp[i]);
nd1 = dfridr(func, xp[i], 0.1, &err);
//nd2 = dfridr((*func), x, 0.1, &err);
fprintf(fp, "%f t %f t %f t n", xp[i], yp[i], nd1);
if(y > max)
max = y;
}
free((void *)xp);
free((void *)yp);
fprintf(fp2, "The minimum value of f(x) is %f when x is between 0 and 20. n", min);
fprintf(fp2, "The maximum value of f(x) is %f when x is between -5 and 5. n", max);
fclose((void *)fp);
fclose((void *)fp2);
return 0;
}
修改后的指针 xp、yp 如下:
float * xp = malloc(((b - a) * pre + 1) * sizeof(float));
float * yp = malloc(((b - a) * pre + 1) * sizeof(float));
经过这些修改后,问题仍然存在。
整个代码中的最新修改,但问题仍然存在:
// The compilation command used is given below
// gcc Q3.c nrutil.c DFRIDR.c -lm -o Q3
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "nr.h"
#define LIM1 20.0
#define a -5.0
#define b 5.0
#define pre 100.0 // This defines the pre
/* This file calculates the func at given points, makes a
* plot. It also calculates the maximum and minimum of the func
* at given points and its first and second numerical derivative.
*/
float func(float x)
{
return exp(x / 2) / pow(x, 2);
}
int main(void)
{
FILE *fp = fopen("Q3data.dat", "w+"), *fp2 = fopen("Q3results.dat", "w+");
int i; // Declaring our loop variable
float min, max, err, nd1, nd2;
// These arrays are defined so that we can pass them into Numerical Recipes routines
//printf("%d n", (int)((b - a) * pre + 1));
float * x = malloc((int)(LIM1 * pre) * sizeof(float));
float * y = malloc((int)(LIM1 * pre) * sizeof(float));
float * xp = malloc((int)((b - a) * pre + 1)* sizeof(float));
float * yp = malloc((int)((b - a) * pre + 1)* sizeof(float));
if (xp == 0 || yp == 0 || x == 0 || y == 0)
{
printf("ERROR: Out of memoryn");
return 1;
}
float yp1 = dfridr(func, a, 0.1, &err), ypn = dfridr(func, b, 0.1, &err);
// Define the initial value of the func to be the minimum
min = func(0);
for(i = 0; x[i] < LIM1 ; i++)
{
x[i] = i / pre; // There is a singularity at x = 0
y[i] = func(x[i]);
if(y[i] < min)
min = y[i];
fprintf(fp, "%f t %f n", x[i], y[i]);
}
fprintf(fp, "nn");
max = 0;
for(i = 0; xp[i] < 5.0; i++)
{
xp[i] = a + i / pre;
yp[i] = func(xp[i]);
nd1 = dfridr(func, xp[i], 0.1, &err);
fprintf(fp, "%f t %f t %f t n", xp[i], yp[i], nd1);
if(yp[i] > max)
max = yp[i];
}
free((void *)x);
free((void *)y);
free((void *)xp);
free((void *)yp);
fprintf(fp2, "The minimum value of f(x) is %f when x is between 0 and 20. n", min);
fprintf(fp2, "The maximum value of f(x) is %f when x is between -5 and 5. n", max);
fclose(fp);
fclose(fp2);
return 0;
}
这个循环永远不会结束,因为x
和b
永远不会被修改:
for(i = 0, x = a; x < b; i++)
{
xp[i] = a + i / pre;
yp[i] = func(xp[i]);
nd1 = dfridr(func, xp[i], 0.1, &err);
//nd2 = dfridr((*func), x, 0.1, &err);
fprintf(fp, "%f t %f t %f t n", xp[i], yp[i], nd1);
if(y > max)
max = y;
}
因此,即使使用更新的xp
和yp
指针,在某个时间点,您的代码开始写入超出您分配的内存限制,因此您将获得未定义的行为。您很幸运glibc检测到了这一点(您可能破坏了它的一些内部数据结构)。