Sign in to follow this  
bentaberry

Quick pointers delete question

Recommended Posts

Hi guys, I am just learning pointers just now in c++ I initialize a pointer in main to the address of a variable and then I pass the pointer into a function argument. Do i delete the pointer after use in the function or in main? Kind Regards David

Share this post


Link to post
Share on other sites
I assume you mean you're doing something like:

T* ptr = &variable;

in which case you don't delete it. You only delete things that you create using 'new'. Also note that instead of doing:

T* ptr = &variable;
someFunction(ptr);

You can just do:

someFunction(&variable);

Share this post


Link to post
Share on other sites
you delete what you new
and you delete[] what you new[]
if all you did was


int foo;
int *pBar = &foo;
dostuff( pBar );


Then you are pointing to a variable on the stack, and you don't need to do anything special with the pointer, other than NOT keep using it after foo goes out of scope.
For instance, this is bad:

int *pBadPointer;
{
int foo;
pBadPointer = &foo;
}
dostuff( pBadPointer );

foo is out of scope at the point dostuff is called, so pBadPointer is pointing to something random on the stack.

Share this post


Link to post
Share on other sites
Ah ok, dont think i need to delete anything with this simple test app im making then.

px: I have done basically the same app im writing now using references as I was learning how they work too and now just getting to grips with pointers and rewriting again.

Thanks for the info guys :)

oh and lets say I was using "new" would i delete the pointer in the function or in the main? I am sure I will be getting to that stage tomorrow :)

Kind Regards
David

Share this post


Link to post
Share on other sites
Quote:

oh and lets say I was using "new" would i delete the pointer in the function or in the main? I am sure I will be getting to that stage tomorrow :)

Thats the problem with pointers. Who owns it?
You can delete it in main, or in the function. Doesn't really matter other than the fact that you can't use the pointer after you delete it. So people would tend to say that the creator takes care of deletion, thus you'd new/delete from main() not from your function.
When you get to the part of using new/delete you may also want to take a good look at smart pointers and weak pointers like those provided by Boost. It helps you to keep track of the "who owns this" question.

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
Quote:

oh and lets say I was using "new" would i delete the pointer in the function or in the main? I am sure I will be getting to that stage tomorrow :)

Thats the problem with pointers. Who owns it?
You can delete it in main, or in the function. Doesn't really matter other than the fact that you can't use the pointer after you delete it. So people would tend to say that the creator takes care of deletion, thus you'd new/delete from main() not from your function.
When you get to the part of using new/delete you may also want to take a good look at smart pointers and weak pointers like those provided by Boost. It helps you to keep track of the "who owns this" question.


Thanks for the headsup, I will deffinately look into the smart weak pointer thing as soon as i get to that stage.

Kind Regards
David

Share this post


Link to post
Share on other sites

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