-1

I want to replace delete and delete[] with _delete ptr; I need it because delete method doesn't set ptr to NULL. For example now I'm using this code for memory clean up:

#define _delete( ptr )    { delete ptr; ptr = NULL; }
#define _deleteA( ptr )   { delete [] ptr; ptr = NULL; }

MyClass *cls = new MyClass();
int *value = new int value[100];
....
_delete(cls);
_deleteA(value);

But I want to use my function like:

_delete cls;
_delete value;

Is it possable?

Or if not how to replace it with:

_delete cls;
_delete[] value;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dmitry
  • 536
  • 6
  • 10
  • `class` is a keyword in C++. – Kerrek SB Nov 11 '14 at 16:30
  • I doubt that's valid syntax – Rad1 Nov 11 '14 at 16:30
  • 3
    No, it's not possable. Neither of them is. – molbdnilo Nov 11 '14 at 16:34
  • It's also a rather pointless exercise, as it solves only a tiny subset of pointer-related problems, and only the easy ones at that. Consider what happens when you have several pointers to the same memory. – molbdnilo Nov 11 '14 at 16:41
  • 2
    Use vectors and smart pointers and you don't have to write delete ever again. – Neil Kirk Nov 11 '14 at 16:48
  • @Neil [Is it good practice to NULL a pointer after deleting it?](https://stackoverflow.com/questions/1931126/is-it-good-practice-to-null-a-pointer-after-deleting-it) contains all around good advice that says "**don't do this**", but I'm not sure if it's a duplicate. –  Nov 11 '14 at 16:50
  • Consider that an invalid pointer could not be NULL; it could point to anywhere in the processor's address space, especially where there is no memory allocated. – Thomas Matthews Nov 11 '14 at 18:11
  • Pointers should be deleted in destructors. There's no point setting the pointer to NULL in a destructor, as the pointer immediately ceases to exist, so it also ceases to be NULL. – MSalters Nov 12 '14 at 09:29

2 Answers2

3

This isn't a good idea. Macros are not the proper tool for the job. Aside from the fact that it probably isn't possible, it will be confusing to anyone maintaining your code. There are several superior alternatives.

If you have C++11, you should prefer to use smart pointers such as std::unique_ptr and std::shared_ptr whenever possible. MatthiasB also makes a valid point that you should opt to take advantage of the standard library and use std::vector instead of dynamically allocated arrays.

Otherwise, you can take advantage of tools like valgrind and LLVM's AddressSanitizer. It's available in Clang and has been ported to GCC 4.9. To use it, specify -fsanitize=address at the command line.

If you insist on textual replacement, a manual search-and-replace in your favorite IDE might do the trick. I wouldn't recommend it though.

A discussion in Is it good practice to NULL a pointer after deleting it? reveals that this practice is indicative of possible design problems in your program. For example, jalf's answer:

Setting pointers to NULL after you've deleted what it pointed to certainly can't hurt, but it's often a bit of a band-aid over a more fundamental problem: Why are you using a pointer in the first place? I can see two typical reasons:

  • You simply wanted something allocated on the heap. In which case wrapping it in a RAII object would have been much safer and cleaner. End the RAII object's scope when you no longer need the object. That's how std::vector works, and it solves the problem of accidentally leaving pointers to deallocated memory around. There are no pointers.
  • Or perhaps you wanted some complex shared ownership semantics. The pointer returned from new might not be the same as the one that delete is called on. Multiple objects may have used the object simultaneously in the meantime. In that case, a shared pointer or something similar would have been preferable.

My rule of thumb is that if you leave pointers around in user code, you're Doing It Wrong. The pointer shouldn't be there to point to garbage in the first place. Why isn't there an object taking responsibility for ensuring its validity? Why doesn't its scope end when the pointed-to object does?

Community
  • 1
  • 1
  • not to forget `std::vector` for dynamic arrays instead of direct pointer allocation wherever applicable. – MatthiasB Nov 11 '14 at 16:45
2

It's not possibe. However, you can look at the answer posted Here that might be useful.

Community
  • 1
  • 1
Rad1
  • 308
  • 2
  • 9