| title | Pointers (C++) | |
|---|---|---|
| ms.date | 11/08/2019 | |
| description | About raw pointers and smart pointers in Microsoft C++. | |
| helpviewer_keywords |
|
|
| ms.assetid | 595387c5-8e58-4670-848f-344c7caf985e |
A pointer is a variable that stores the memory address of an object. Pointers are used extensively in both C and C++ for three purposes:
- to allocate new objects on the heap,
- to pass larger objects to functions,
- to pass functions to other functions
- to iterate over elements in arrays or other data structures.
In C-style programming, raw pointers are used for all these scenarios. However, pointer errors are by far the greatest cause of crashes, hangs, data corruption, security holes, and general programmer misery. In modern C++, the use of raw pointers is strongly discouraged except in scenarios where they provide a significant performance benefit. Modern C++ provides smart pointers for allocating objects, iterators for traversing data structures, and lambda expressions for passing functions. By using these language and library facilities instead of raw pointers, you will make your program safer, easier to debug, and simpler to understand and maintain. See Smart pointers, Iterators, and Lambda expressions for more information.