2

I am new to c++ coding and this question may seem childish to you all. But I am really not able to come up on a solution to my problem. Please help.

#include <iostream>
using namespace std;

int* getnew(int* x){
   int temp = *x;
   int* y = &temp;
   cout << "from function address of y = " << y
        << " and value of y = " << *y << endl;
   return y;
}

int main(){
    int x = 100;
    int* y = getnew(&x);
    cout << "address of y = " << y;
    cout << " and value of y = " << *y << endl;
    return 0;
}

Output of this code :

from function address of y = 0x7ffee52aa914 and value of y = 100
address of y = 0x7ffee52aa914 and value of y = 1

What I thought should be the output :

from function address of y = 0x7ffee52aa914 and value of y = 100
address of y = 0x7ffee52aa914 and value of y = 100

Can someone explain where am I going wrong ?

Tushar Vatsal
  • 39
  • 2
  • 13
  • Local variables are destroyed when a function returns – M.M May 14 '18 at 12:09
  • You are returning the address of the _local_ variable `temp`. Local variables cease to exist as soon as you exit the scope where they are declared. So the behaviour of your program is undefined. – Jabberwocky May 14 '18 at 12:09
  • There *ought* to be a duplicate of this, but I can't find it. There are lots about a compiler warning (but that is not what the OP is asking about), there are a few about C (but C and C++ are different languages - the fact the rules are the same in this area is not something the OP can be expected to know), there are a few about returning references (but references are not pointers). – Martin Bonner supports Monica May 14 '18 at 12:13
  • 2
    int tmp = *x causes a local variable to be created on the stack. Use auto& instead if you want to change the value of x and use a reference. Use new if you want to return a locally created object. This object will have to be cleared manually using delete. Instead of using new you can use smart pointers. (std::shared_ptr) – Gladaed May 14 '18 at 12:14
  • 1
    Replace `int temp = *x;` with `static int temp = *x;` and it will will work as you expect because the static `temp` variable will continue to exist even once you've exited the `getnew` function. This is not good practice however, it's just for illustration. – Jabberwocky May 14 '18 at 12:15
  • 3
    @Tushar: Welcome to Stack Overflow by the way. Congratulations on writing a complete [MCVE] for your first question - I wish that was commoner. – Martin Bonner supports Monica May 14 '18 at 12:16

1 Answers1

1

Since you’re getting the address of temp, wich was declared inside a function, it’s scope belongs only to the function.

So, when you leave get_new() the temp var no longe exists ( get deallocated ). Then when you acess the same address ( the temp address) in main you access a space of memory that isn’t reserved, wich means that that space can be freely used by other vars and stuff of the program.

Try to use malloc to allocate a new space of memory and guarantee that the var still lives after the get_new returns (finishes). Here’s a example:

#include <iostream>
using namespace std;

int* getnew(int* x){
   int temp = *x;
   // return a memory address pointing to a ‘ sizeof(int )’ bytes reserved space.
   int  *y = malloc( sizeof(int) ); 
   cout << "from function address of y = " << y << " and value of y = " << *y << endl;
   return y;
}

int main(){
    int x = 100;
    int* y = getnew(&x);
    cout << "address of y = " << y;
    cout << " and value of y = " << *y << endl;
    return 0;
}

EDIT: Also do not forget to deallocate the memory when you're finished using it ( no longer needs it):

#include <iostream>
using namespace std;

int* getnew(int* x){
   int temp = *x;
   // return a memory address pointing to a ‘ sizeof(int )’ bytes reserved space.
   int  *y = malloc( sizeof(int) ); 
   cout << "from function address of y = " << y << " and value of y = " << *y << endl;
   return y;
}

int main(){
    int x = 100;
    int* y = getnew(&x);
    cout << "address of y = " << y;
    cout << " and value of y = " << *y << endl;

/*********** same code until here********/

    //use free for each memory allocated by malloc()
    free(y);    //deallocates the memory pointed by *y*
    free(other_y_if_it_exists);

    return 0;
}