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

Ben
  • 133
  • 3

11 Answers11

17

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
  • 1,112
  • 3
  • 14
  • 26
  • 12
    Correct. In fact you **must not** - attempting to `delete` the same memory block twice would lead to undefined behaviour. – Péter Török May 17 '10 at 21:20
  • 4
    It should also be noted that any attempt to deference `pointer2` will result in undefined behavior, as well. – Nathan Ernst May 17 '10 at 21:23
  • 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 May 17 '10 at 22:48
  • Unfortunately it would not help with `pointer2` here, a good guideline still. – Matthieu M. May 18 '10 at 06:16
7

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
  • 24,346
  • 3
  • 50
  • 88
4

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
  • 84,970
  • 20
  • 145
  • 172
2

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
  • 35,646
  • 15
  • 94
  • 131
2

Additionally, consider using boost::shared_ptr<> from the Boost libraries. It's the greatest thing since sliced bread.

typedef boost::shared_ptr<TypeX> 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 built-in auto_ptr<> type, if you don't need a reference count:

typedef auto_ptr<TypeX> 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++.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
Vagrant
  • 1,696
  • 12
  • 19
1

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

Cogwheel
  • 22,781
  • 4
  • 49
  • 67
0

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

Robben_Ford_Fan_boy
  • 8,494
  • 11
  • 64
  • 85
0

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
  • 50,859
  • 17
  • 87
  • 112
0

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

0

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.

M. Williams
  • 4,945
  • 2
  • 26
  • 27
  • 2
    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 May 17 '10 at 21:24
0

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
  • 234,037
  • 30
  • 302
  • 389