Sign in to follow this  

Passing pointers as function parameters

This topic is 3296 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a got a function:
void CMultiset< Type >::AllocateMemory(int Size, CObject<Type> *front, CObject<Type> *back )
{
	//Create the first object, make it the front AND back
	CObject< Type >* obj = new CObject< Type >(NULL, NULL, 0);
	front = back = obj;

	CObject< Type >* obj2;

	//Allocate memory for the size indicated
	for(int i=1; i < Size; i++)
	{
		obj2 = obj;
		obj = new CObject< Type >( obj2, NULL, i );
		obj2->m_Left = obj;

		front = obj;
	}
}
The function works fine in itself, the front and back pointer are allocated correctly, but for some reason it is not updating the pointers I have passed to the function. I'm sure it'll be simple but can someone help please?

Share this post


Link to post
Share on other sites
The pointers themselves are passed by value not by reference, so you're only modyfing the local copies of the pointers. You probably want something like void CMultiset< Type >::AllocateMemory(int Size, CObject<Type> *&front, CObject<Type> *&back )

Share this post


Link to post
Share on other sites

void A(int i)
{
i = 3;
}

int foo = 2;
A(foo); // foo is still 2... why?

void B(float* f)
{
*f = 3.0f;
}

float bar = 2.0f;
B(&bar); // now bar is 3.0... why?

void C(float* p)
{
p = new float;
}

float zzz;
float* baz = &zzz;
C(baz); // baz still points to zzz... why?

Share this post


Link to post
Share on other sites

This topic is 3296 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this