tags:

views:

240

answers:

8

I have a pointer that points to an array and another pointer referencing the same array. How do i delete any one of those pointers without killing the array such that the second undeleted pointer still works?

for example:

int* pointer1 = new int [1000];
int* pointer2;
pointer2 = pointer1;

Now i want to get rid of pointer1, how would i do it such that i can continue to access the array normaly through pointer2?

+7  A: 

Let it go out of scope?

You don't 'delete' pointers, you delete what they point at. So if you just want to get rid of the variable 'pointer1' the only way to do that is for the scope it was created in to be terminated.

Noah Roberts
+6  A: 

You don't pass that pointer to delete at all. You just stop using that pointer. If you want to make sure you don't access the array through that pointer again, you can set it to null.

C and C++ do not keep track of how many pointers point to an object or an array. If you want reference counting, you need to use a reference-counted container, like shared_ptr, or in this case, shared_array (you can find both of those in Boost and there is a high likelihood that your implementation already has them in <memory> either in the std namespace or in std::tr1).

James McNellis
+7  A: 

Those pointers are on the stack; you don't have to delete them. Just ignore pointer1 and it will go away at the end of the block.

Cory Petosky
Ahh this explains it the best to me. So a pointer is simply a variable on the stack, that makes sense. So this means it can't be accessed out of scope and i won't accidentally use it elsewhere out of scope and cause problems. Ok, thank you.
Faken
@Faken- To be clear, "a pointer is simply a variable on the stack" is not a true statement. The pointer in your code sample is a stack variable because you declared it statically. A pointer can also be declared dynamically, as Neil Butterworth shows in his example. It's all in how you declare it, there's nothing special about the object being a pointer.
bta
@bta: I don't see anything statically declared. Perhaps you mean "automatic", the actual term. (That is, things are not stack allocated, but they are automatically allocated.)
GMan
The compiler is allowed to "make the pointer go away" as soon as the compiler deems it not in use. A pointer may go away before the end of the block.
Thomas Matthews
+1  A: 
int** pointer1 = new int *;
* pointer1 = new int [1000];
int* pointer2;
pointer2 = * pointer1;
delete pointer1;
anon
Heh, you've been very helpful to me in the past, but this time you've just confused me instead, heh.
Faken
@Faken Consider it a Zen lesson :-) Basically, as others have pointed out, your question as asked does not make too much sense.
anon
@Faken- He's showing you what you would have to do if you wanted to be able to delete the pointer. He creates a "pointer-to-a-pointer" and assigns it the address of a `int*` created through `new`. Since `pointer1` is allocated dynamically in this case, you can `free` it once you are done with it. The complexity of this solution probably indicates that you need to re-think whether you really need to delete the pointer.
bta
@Neil: Ignorance is bliss. That's what happens to me when i get thrown off the deep end. Not having any having any real programming experience or training before jumping into a massive programming project tends to have that effect. Notice how all my reputation is earned from pure up votes to my questions...heh...Needless to say, stackoverflow has saved my life.
Faken
+1  A: 

It's a stack variable (the way you've written it) and therefore bound to the scope you declared it in. Once you hit the closing curly brace of that particular scope, it will get cleaned up without you having to do anything about it.

If you want to make absolutely sure nothing else can use pointer1 any longer, you could set pointer1=NULL once you're done with it.

Brent Nash
+1  A: 

When you statically declare a pointer with int* pointer1;, you can't free it. You can only free dynamically-allocated memory that you allocated using new, malloc, etc. The only way to "get rid of" pointer1 is to let it go out of scope, such as when the function returns. If you are done using pointer1 and want to prevent it from accidentally being used to alter the array, use pointer1 = NULL;.

bta
A: 

Just ignore the pointer, and it will go away when you're done with your function or block. Just remember that once you no longer have a pointer to something, it won't be able to be accessed. Of course, it will still be there using up memory, but you won't be able to get to it. So, before you consider letting go of the pointer, make sure you either have another pointer to it somewhere, or you get rid of it.

glowcoder
+2  A: 

Normally to make a pointer "safe" just set it to NULL which makes it point to nothing at all. Or you can just let it go out of scope.

For example, if you have two pointers.

int *a = new int;
int *b = a;

// somewhere    
b = NULL;

delete b; // does nothing now
delete a; // deletes a

Or you can let it fall out of scope.

int *a = new int;

{
 int *b = a;
 // blah blah blah
} 

// don't have to worry about b
delete a;
Daniel
Does `b = NULL; delete b` do anything? Is it defined? I was under the impression that that's some sort of bug or undefined behavior.
Nathan Fellman
@Nathan: `foo = NULL; delete foo;` is pefectly legal and harmless. Means you don't have to always look for non-NULL-ness before deleting.
Johann Gerell