local var

hey,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>


class Counter
{
private:
	int val;
public:

	Counter()
	{
		val = 0;
	}

	int get_val() const
	{
		return val;
	}

	void set_val(int x)
	{
		val = x;
	}

	void Increment()
	{
		++val;
	}

	Counter& operator++()
	{
		++val;
		Counter temp;
		temp.set_val(val);
		return temp;
	}
};

int main()
{
	Counter i;
	i.Increment(); // 1

	++i;
	std::cout << i.get_val() << std::endl; // 2

	Counter* a = &( ++i );
	a->set_val(5);
	std::cout << a->get_val() << std::endl; // 5

return 0;	

}


when I call:std::cout << a->get_val() << std::endl; the local variable temp still exist. what they teached me that in c++ the local variable will be deleted after exiting from Func. any explanation guys?
thanks
1
2
3
4
int get_val() const
	{
		return val;
	}

This function returns an integer. Now, when it returns, the return value is copied so the value received by the caller is not the same variable, but a copy thereof. Hence, although any local variables in the called function are no longer alive, the return value will have been copied out successfully.

I'm not sure what you mean by this:
the local variable temp still exist

The variable temp is never attempted to be accessed outside the function Counter::operator++().

Also, look at this code:
1
2
3
4
5
6
7
Counter& operator++()
	{
		++val;
		Counter temp;
		temp.set_val(val);
		return temp;
	}


You are returning a reference to a temporary object, so - for the very reason you have posted - the return value is useless. Instead return by value (i.e. change Counter& to Counter. Note also that instead of needlessly creating an additional copy, you can just do this:
1
2
3
4
5
Counter operator++()
	{
		++val;
		return *this; // deference the pointer to the current class and return that value
	}

Note that the copy induced by returning by value is often optimised out by the compiler, a process called return value optimisation.
http://en.wikipedia.org/wiki/Return_value_optimization
thanks for Quick replay.
Let me Rewrite the function,"
1
2
3
4
5
6
7
8
	Counter& operator++()
	{
		++val;
		Counter temp;
		temp.set_val(val);
		std::cout<<std::endl<<&temp<<std::endl;
		return temp;
	}
"
I RETURN REFERENCE FOR LOCAL VARIABLE WHICH IS "temp".
When I call from main (little changes in it)"
1
2
3
4
5
6
7
8
9
10
11
12
int main(){
	Counter i;
	i.Increment(); // 1

	++i;
	std::cout << i.get_val() << std::endl; // 2 

	Counter* a = &( ++i );// I get pointer to the returned reference of local variable temp
	std::cout<<std::endl<<a<<std::endl;
	std::cout << a->get_val() << std::endl; //           // well it print '3'. temp still exist isn't it?
return 0;	
}
"
and my output:
1
2
3
4
5
6
7
8
9
./byval 

0xbfc0462c
2

0xbfc0462c

0xbfc0462c
3


local variable still exist after I finish the function. !!!
No, the local variable no longer exists. You are not reading data from temp because temp isnt' there anymore. What you're reading is garbage/leftover RAM. This is very unsafe to do.

EDIT:

I removed my example because whether or not is exposes the problem depends entirely on the compiler's optimization settings.

Just trust us, 'temp' no longer exists. By returning a reference like that, you are doing "very bad things". 'a' is a bad pointer.
Last edited on
Topic archived. No new replies allowed.