Use this macro to help declaring auto-release pointers in C++. See usage in the source. Remember that std::auto_ptr is a standard template class that helps releasing memory, since the pointer you provide in the constructor will be released in the destructor of the class. This means that memory is released "automagically" when the variable goes out of scope. Use of this class is specially advisable in a method with serveral returns or capable of throwing an exception.
- #include <memory> // to use auto_ptr
- // Macro: N stands for Name, L for Lenght and T for Type
- #define AUTO_PTR(N, L, T) T* p##N = new T[L]; std::auto_ptr<T> auto_##N(p##N);
- // Sample usage
- AUTO_PTR(cFileName, MAX_PATH, char);
- gets(pcFileName); // Pointer can be accessed with "p" as a prefix.
You need to login to post a comment.