Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Address of local variable ’x’ returned
Forum Updated to NodeBB v4.3 + New Features

Address of local variable ’x’ returned

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    uhlajs
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      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

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andreyc
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved