I have a pointer that poi开发者_开发技巧nts 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?
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.
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.
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
).
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;
.
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;
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.
int** pointer1 = new int *;
* pointer1 = new int [1000];
int* pointer2;
pointer2 = * pointer1;
delete pointer1;
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.
精彩评论