views:

290

answers:

11
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!

+11  A: 

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.

Nilbert
Correct. In fact you **must not** - attempting to `delete` the same memory block twice would lead to undefined behaviour.
Péter Török
It should also be noted that any attempt to deference `pointer2` will result in undefined behavior, as well.
Nathan Ernst
side.. As you're learning.. I'd say it's a good idea to set pointer1 = NULL; As a reflex to the delete. I've seen a pile of code where the pointer is deleted but later the app does a if(pointer1)..
baash05
Unfortunately it would not help with `pointer2` here, a good guideline still.
Matthieu M.
A: 

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

David Relihan
A: 

delete deletes the memory that was allocated by new. Since you only have one new you only need one delete.

Cogwheel - Matthew Orlando
+6  A: 

A simple rule: you need as many deletes as there are news. 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.

Nemanja Trifunovic
note that pointer1 also becomes a dangling pointer after the delete...
Greg Rogers
Very true, but I thought it was more obvious :)
Nemanja Trifunovic
A: 

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.

nico
Nathan Ernst
@Nathan Ernst: Sorry, I mistyped. Corrected
nico
A: 

Every new should have one, and only one, matching delete. If you deleted the other pointer, you would break that rule.

anon
A: 

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.

Kotti
It would be undefined behavior if he deleted pointer2. In practice, I don't know a single platform where it would result in a memory leak. Most likely, you'd get something like segfault/AV.
Nemanja Trifunovic
+1  A: 

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.)

wheaties
+1  A: 

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.

cHao
A: 

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.

Chuck
+1  A: 

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++.

Vagrant