2014-01-12 80 views
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; 
} 
+0

所有的'malloc's一起出现,对吧?尝试使用调试器精确定位消息的显示时间。也许当你打电话免费? – zmbq

+1

因此,尽管标题,malloc不返回错误。它看起来像你在两个malloc调用中分配零字节..... – talonmies

+0

'((b-a)/ pre)'小于一个...... – Mat

回答

3

这个循环永远不会结束,因为xb决不会被修改:

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; 
} 

因此,即使更新xpyp指针,有一个时间点你的代码开始写超出你分配的内存限制,所以你得到未定义的行为。你很幸运,glibc检测到了这一点(你可能损坏了它的一些内部数据结构)。

+0

谢谢你指出这一点。我现在正在处理这个问题。 – Vesnog