Quick pointers delete question

Started by
4 comments, last by bentaberry 14 years, 2 months ago
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
Advertisement
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);
Humble people don't refer to themselves as humble (W.R.T. "IMHO".)
you delete what you new
and you delete[] what you new[]
if all you did was

int foo;int *pBar = &foodostuff( 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.
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
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.
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

This topic is closed to new replies.

Advertisement