A pointer is a type that holds an address.
A pointer can be initialized with the keyword new, which reserves free space for the dynamically allocated instance and returns the address to it.
C++ does not free this memory on its own. Therefore, you have to call delete to do so.
Reading/writing from/to an uninitialized pointer results in an access violation.
- Keep use of pointers simple and straightforward [6]
- Initialize a pointer with nullptr [8] (in C++11) or the integer literal 0 [4] (in C++98), rather than NULL.
- Avoid non-trivial pointer arithmetic [5,7]
- Prefer using a smart pointer over a plain pointer [1-3]
- Keep pointers that represent ownership inside handle classes [9-11]
- Use const pointers to express immutability in interfaces [13]
- Prefer references to pointers as arguments, except where "no object" is a reasonable option [14,15]
- Avoid void* except in low-level code [12]
- Only return pointer to dynamic allocated data, data that existed before the function was called, or static data [16]
- Polymorphic types must always be passed by reference or (smart) pointer [17]
- Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 13: 'Use objects to manage resources'.
- Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 17: 'Store newed objects in smart pointers in standalone statements'
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 13: 'Ensure resources are owned by objects. Use explicit RAII and smart pointers.
- Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Section 5.8.3: 'Use 0 rather than NULL'.
- Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Section 5.8.1: 'Avoid non-trivial pointer arithmetic'.
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice, page 199: '[1] Keep use of pointers simple and straightforward'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice, page 199: '[2] Avoid nontrivial pointer arithmetic'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice, page 199: '[5] Use nullptr rather than 0 or NULL'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice, page 199: '[11] Keep pointers that represent ownership inside handle classes'
- Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 13: 'Use objects to manage resources'.
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 13: 'Ensure resources are owned by objects. Use explicit RAII and smart pointers.
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice, page 199: 'Avoid void* except in low-level code'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice. page 199: '[13] Use const pointers and const references to express immutability in interfaces'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice. page 199: '[14] Prefer references to pointers as arguments, except where "no object" is a reasonable option'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 12.7. Advice. page 341: '[11] Pass a pointer if "no object" is a valid alternative (and represent "no object" by nullptr)'
- Gottschling, Peter. Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers. Addison-Wesley Professional, 2015. Chapter 1.8.6: 'Only return pointers and references to dynamic allocated data, data that existed before the function was called, or static data'
- Gottschling, Peter. Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers. Addison-Wesley Professional, 2015. Chapter 6.1.3: 'Polymorphic types must always be passed by reference or (smart) pointer!'