Return to Snippet

Revision: 10191
at December 12, 2008 03:25 by jarnaldich


Updated Code
#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.

Revision: 10190
at December 12, 2008 03:17 by jarnaldich


Initial Code
#include <memory> // to use auto_ptr

// Macro
#define MAKE_BUFFER(N, L) char* p##N = new char[L]; std::auto_ptr<char> auto_##N(p##N);

// Sample usage
MAKE_BUFFER(cFileName, MAX_PATH);
gets(pcFileName); // Pointer can be accessed with "p" as a prefix.

Initial URL
http://www.cppreference.com/wiki/stl/memory/auto_ptr

Initial Description
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.

Initial Title
Macro to ease auto_ptr usage

Initial Tags

                                

Initial Language
C++