Do I need to delete pointer arguments

I'm quite new to C++ so I'm sorry if this is quite a novice question but basically when I create a pointer argument like this:
 
  void Animation::Update(float* deltaTime)

Do I need to delete that argument using the delete keyword? like you would with any other pointer?
Last edited on
You would need to delete a pointer with delete only if you create it with new.
Do I need to delete that argument using the delete keyword?

No.

The only exception to that would be if deltaTime was created with new and it was the responsibility of Update to return the memory (unlikely, and a poor design).

like you would with any other pointer?

Just because something is a pointer does not mean you should call delete. A pointer is simply a variable that contains a memory address. What is pointed to should only be deleted if it was created with new.
very old code or C code being worked into c++ may have functions that expect the user to delete the data. This "should" be part of the comments and documentation for the functions. It is not done this way anymore, but you may see it.

example

int * foo(int x)
{
int * ip = new int[x];
return ip;
}

you would need to delete the result of this sort of function after using the pointer. Thanks to vectors, you would not see this style as much in new code.
At risk of reiterating what has already been said:

like you would with any other pointer?


If you find yourself routinely deleting every pointer, something has gone horribly horribly wrong and you need to calmly get to the exit and walk away. Don't take your bag, that'll tip them off that you're making a break for it.
Pointers are a mere implementation detail.

The real question should be: How to manage memory?
The essence of the answer is: Properly. Diligently. Meticulously.

You have to know what you have to manage.


C++ Standard Library has generic Container types and Smart Pointer types. They are very helpful in memory management.
Topic archived. No new replies allowed.