Address of local variable ’x’ returned



  • Hello, please help me. I can't fing mistake but I know that there is! :D
    I have a simple app. This app contain two file arith.h and main.cpp

    arith.h
    @#ifndef ARITH_H
    #define ARITH_H

    #include <math.h>

    float* quadratic_equation(float coefficient[]) {
    // promene
    float d, x[2];
    // vypocet
    d = pow(coefficient[1],2)-4coefficient[0]coefficient[2];
    x[0]=((-coefficient[1])+ sqrt(d))/(2
    coefficient[0]);
    x[1]=((-coefficient[1])- sqrt(d))/(2
    coefficient[0]);
    return x;
    }
    float* two_equations(float coefficient_1[], float coefficient_2[]) {
    // promene
    float cal_1 = coefficient_1[1], cal_2=coefficient_2[1], x[2];
    // vypocet
    for (int i=0; i <=2; i++)
    {
    coefficient_1[i]=cal_2;
    coefficient_2[i]
    =cal_1;
    coefficient_1[i]-=coefficient_2[i];
    }
    x[0]=coefficient_1[2]/coefficient_1[0];
    x[1]=(coefficient_2[2]-coefficient_2[0]x[0])/coefficient_2[1];
    return x;
    }
    float
    three_equations(float coefficient_1[], float coefficient_2[], float coefficient_3[]) {
    //promene
    float *pointer = NULL;
    float cal_1[4], cal_2[4], x[3];
    pointer = cal_2;
    // vypocet
    for (int I = 0; I < 2; I++)
    {
    for (int i = 0; i <= 3; i++)
    {
    cal_1[i] = coefficient_1[i]*pointer[0];

        }
        for (int i = 0; i <= 3; i++)
        {
            pointer[i] *= coefficient_1[0]*(-1);
            pointer[i] += cal_1[i];
        }
        pointer = coefficient_3;
    }
    for (int i = 0; i <= 3; i++)
    {
        cal_2[i] = coefficient_2[i]*coefficient_3[1];
    }
    for (int i = 0; i <= 3; i++)
    {
        coefficient_3[i] *= coefficient_2[1]*(-1);
        coefficient_3[i] += cal_2[i];
    }
    x[0] = coefficient_3[3]/coefficient_3[2];
    x[1] = (coefficient_2[3]-coefficient_2[2]*x[0])/coefficient_2[1];
    x[2] = (coefficient_1[3]-coefficient_1[2]*x[0]-coefficient_1[1]*x[1])/coefficient_1[0];
    return x;
    

    }

    #endif // ARITH_H
    @

    main.cpp
    @#include <iostream>
    #include "arith.h"
    using namespace std;

    int main()
    {
    cout << "rovnice";
    float pole[3], pole1[3];
    pole[0]=4;
    pole[1]=3;
    pole[2]=6;
    pole1[0]=2;
    pole1[1]=1;
    pole1[2]=4;
    cout << two_equations(pole, pole1)[1];
    cout << two_equations(pole, pole1)[0];
    cout << two_equations(pole, pole1)[1];

    return 0;
    

    }

    @

    Problem is that the third call of function two_equations give me "nan" result. But it not true. So where i do mistake?



  • Hi,

    in two_equations you return a pointer to a local variable (x); this is wrong because outside of your function, the memory address could be reused and then you access to wrong data.

    Usually, if you need to return a value you MUST choose one of these techinques:

    return by value (stack value)

    return a dinamically allocated value (heap)

    modify a input parameter address



  • Local variables are destroyed when control flow leaves the function, so the result is lost and you are getting some random data.
    I would suggest to allocate memory inside the function and then deallocate it in the main or pass pointer or reference to x to the function and fill it with the result.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.