I cannot trace my pointer

hello,
here is the same stuation that I lived with my project. I am sending a variable to my function, function is doing something with a temp variable and than assing temp's address to the original variable which is passed to the function. However, when the compiler exit from that function's scope, everything becomes unchanged ? by the way sorry for my bad English.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
float* x = new float();
template<class t>
inline void fillfunc(t* p)
{
	t* d = new t[10];
	for (int i=0;i<10;i++)
	{
		d[i] = i*2;
	}
	p = d; // x is being changed here
}

void testfunc()
{
	fillfunc(x); // passing x as argument	
	cout<<x[3]<<endl; // result x wasnt changed
}

void main()
{
	testfunc();
}
Last edited on
I am going to go out on a limb and say that I think you should try this:

Do not allocate memory for float* x. Just define it. float* x; Otherwise you just need to delete that memory before you re-assign the value of x.

Next, try adding & to your fillfunct() so it is fillfunct(t*& p) because you are passing in a pointer and you want to change the value of that pointer, so you must do it by reference.

~psault
Last edited on
thank you, it is working now.
Last edited on
Topic archived. No new replies allowed.