1

If I create 2 pointers:

int *pointer;
int *temp;

and I allocate memory for one of them in this case temp:

temp = new int [ size ]; //size = 6

I then point my second pointer pointer to the same memory location:

pointer = temp;

If I want to deallocate temp:

delete [ ] temp;

it won’t let me… And I say it won’t let me because if I do a loop to populate elements of temp it will still let me populate it even after I used delete [ ] temp;. I am trying to understand what happens here so now my questions are:

  1. When I did delete [ ] temp; was the memory deallocated (I think not because I had another pointer pointing to that location) and if it wasn’t… does that means the delete [ ] temp; had no effect?
  2. Is it possible to deallocate memory if more than one pointer is pointing to that memory location?
  3. Lets say that i am done with both pointers and I want to free the memory, what would be the best way to do it? I cannot do delete [ ] pointers; because I never allocated memory for pointer I just pointed it to the temp memory address.

Note: I know that I can use vectors I am just trying to understand memory management while using pointers.

3
  • I don't get an error, program compiles and works fine, I say it won't let me because even after I did delete [ ] temp; I still can get access to that memory location. Commented Oct 7, 2015 at 17:59
  • Watch this video: youtube.com/watch?v=6pmWojisM_E (and read some books) Commented Oct 7, 2015 at 17:59
  • Raw arrays in C++ are not "dynamic" (they do not dynamically grow or shrink), but can be dynamically allocated. Commented Oct 7, 2015 at 18:06

2 Answers 2

4

When I did delete [ ] temp; was the memory deallocated

Of course it was! delete operator is very obedient, it assumes that if you ask it to delete a chunk of memory, then you are done with that chunk for good.

I think not because I had another pointer pointing to that location

The moment you called delete [] on the temp that other pointer became dangling. Dereferencing it is undefined behavior, meaning that your program is invalid, even though it compiles or even runs to completion without a crash.

Is it possible to deallocate memory if more than one pointer is pointing to that memory location?

Yes, that is, in fact, what happens.

Lets say that i am done with both pointers and i want to free the memory, what would be the best way to do it?

Use smart pointers, e.g. std::shared_ptr<T>. This would let you avoid calling delete altogether.

Sign up to request clarification or add additional context in comments.

Comments

1
  1. When I did delete [ ] temp; was the memory deallocated

    Yes it was. Using the memory after deletion causes undefined behavior, i.e. anything can happen, including but not limited to: code compiles, code crashes, code runs as expected, …

    if i do a loop to populate elements of temp I will still let me populate it even after I used delete [ ] temp;

    C++ has no mechanism to prevent use-after-delete. If you're doing that then you're simply invoking undefined behavior.

  2. Is it possible to deallocate memory if more than one pointer is pointing to that memory location?

    Yes. Again, there's nothing in C++ that's going to stop the programmer from doing this. You have to make sure your code is not double-deleting anything or accessing anything that has already been deallocated.

  3. I cannot do delete [ ] pointers; because I never allocated memory for pointer I just pointed it to the temp memory address.

    You can delete the memory via pointer too. Any pointer that points to the allocated memory and has the same type that was used to allocate the memory can be used to delete it.

    Finally, do as dasblinkenlight said: avoid advanced manual memory handling like new and delete, and prefer smart pointers instead. They will automagically deallocate your memory 1) at the right time, and 2) only once. After that they reset themselves to nullptr making use-after-delete easy to spot.

2 Comments

If by "no mechanism" you mean "it ships with a standard library that can trivially handle it" then yes.
@Puppy Smart pointers don't prevent use-after-delete afaik, am I missing something?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.