char *pointer1;
char *pointer2;
pointer1 = new char[256];
pointer2 = pointer1;
delete [] pointer1;
In other words, do I have to do delete [] pointer2
as well?
Thanks!
char *pointer1;
char *pointer2;
pointer1 = new char[256];
pointer2 = pointer1;
delete [] pointer1;
In other words, do I have to do delete [] pointer2
as well?
Thanks!
Nope, that code is fine and won't leak memory.
You only have to use delete[] once because you've only used one new to allocate an area for memory, even though there are two pointers to that same memory.
Only use Delete when you've used New
Good practive is to set pointer2 to NULL, but you won't have a memory leak if you don't
delete
deletes the memory that was allocated by new
. Since you only have one new
you only need one delete
.
A simple rule: you need as many delete
s as there are new
s. Even better, use something like a smart pointer or a container to take care of it for you.
And another minor point: pointer2
is becoming a "dangling pointer" once you call delete
on pointer1
.
No, you do not have to delete[] pointer2 because you have not allocated memory for it!
The statement pointer2 = pointer1
makes pointer2
point to the same memory address as pointer1
, does not allocate any extra memory for it.
Every new
should have one, and only one, matching delete
. If you deleted the other pointer, you would break that rule.
What you do here is just tell that memory block allocated by new char[256]
would be pointed by pointer1
and pointer2
at the same moment.
It would be a memory leak if you wrote delete[] pointer2;
statement after.
While it doesn't leak memory, if you want to be explicit, you should set both point1
and point2
to NULL (and initialize them that way too.)
It's not a leak, but it is asking for trouble. pointer2
is pointing to who-knows-what as soon as you delete pointer1
. It's what's called a "dangling pointer". Using it can in the best case cause a segfault, and in the worst case can cause mysterious data mangling in anything that ends up allocated that same spot.
Here's the gap in your thinking: You don't delete pointers — you delete memory. You simply use a pointer to identify the block of memory to be freed. Since the two variables point to the same memory, deleting one is precisely equivalent to deleting the other. In other words, deleting both is the same as deleting one of them twice — which is obviously wrong.
Additionally, consider using boost::shared_ptr<> from the Boost libraries. It's the greatest thing since sliced bread.
typedef boost::shared_ptr StrRef;
foo() { StrRef pointer1(new TypeX);
while(something) { StrRef pointer2 = pointer1; // do stuff }
return; }
The data (TypeX) will be deleted when the last pointer to it goes out of scope. You can do something similar with the builtin auto_ptr<> type, if you don't need a reference count:
typedef auto_ptr StrRef;
foo() { StrRef pointer1(new TypeX);
while(something) { TypeX * pointer2 = pointer1.get(); subroutine(pointer2); if (condition) return; }
return; }
Whenever pointer1 goes out of scope, it will delete the data. The advantage with this is that you don't have to remember put a delete before the return statement at the bottom, and if pointer1 goes out of scope for any other reason (i.e. return from the middle of the loop, or subroutine() throws an exception, then the data will still be deallocated properly.
I haven't tested this code, so you'll have to check the docs for auto_ptr<> and boost::shared_ptr<> yourself.
I highly recommend using the Boost libraries as much as possible. It's written by pro's it's basically a staging area for extensions to C++.