Vesnog
Vesnog

Reputation: 785

Malloc returning an error

After I allocate an array and free it I have an error message displayed as follows:

*** glibc detected *** ./Q3: malloc(): memory corruption (fast): 0x09a092f8 ***

How can I remedy the situation? What may be the underlying result behind it?

The code used is as follows:

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

Modified pointers xp, yp are as follows:

float * xp = malloc(((b - a) * pre + 1) * sizeof(float));
float * yp = malloc(((b - a) * pre + 1) * sizeof(float));

After these modifications the problem still persists.

Latest modifications throughout the code but the problem is still presents:

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

Upvotes: 0

Views: 98

Answers (1)

Filipe Gon&#231;alves
Filipe Gon&#231;alves

Reputation: 21213

This loop never ends, since x and b are never modified:

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

Thus, even with the updated xp and yp pointers, there is a point in time where your code starts writing beyond the memory limits you allocated, so you get undefined behavior. You are lucky that glibc detected this (you probably corrupted some of its internal data structures).

Upvotes: 3

Related Questions