0
後,我分配數組和自由吧,我還有顯示錯誤消息如下:的malloc返回錯誤
*** 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, "\n\n");
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 memory\n");
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, "\n\n");
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;
}
所有的'malloc's一起出現,對吧?嘗試使用調試器精確定位消息的顯示時間。也許當你打電話免費? – zmbq
因此,儘管標題,malloc不返回錯誤。它看起來像你在兩個malloc調用中分配零字節..... – talonmies
'((b-a)/ pre)'小於一個...... – Mat