Skip to main content

Pointers and References

  • Chapter
  • First Online:
Beginning C++20
  • 3165 Accesses

Abstract

The concepts of pointers and references have similarities, which is why we have put them together in a single chapter. Pointers are important because they provide the foundation for allocating memory dynamically. Pointers can also make your programs more effective and efficient in other ways. Both references and pointers are fundamental to object-oriented programming.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 54.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

     A term that is often used as a synonym for the free store is the heap . In fact, it is probably more common to hear the term heap than free store. Nevertheless, some would argue that the heap and the free store are different memory pools. From their point of view, C++’s new and delete operators operate on the free store, whereas C’s memory management functions such as malloc and free operate on the heap. While we take no stance in this technical, terminological debate, in this book we consistently use the term free store because this is what the C++ standard uses as well.

  2. 2.

     In Chapter 18, you’ll learn that while copying a unique_ptr<> is not possible, you can “move” the address stored by one unique_ptr<> object to another by using the std::move() function. After this move operation the original smart pointer will be empty again.

  3. 3.

     std::make_shared<T>() will typically allocate only a single contiguous memory block that then stores both the shared_ptr’s reference count variable and the shared T value. This is both faster and more economical in memory usage.

  4. 4.

    Not unless you bypass the compiler’s safety checks by using raw pointers, that is, for instance by using new or get(). Mixing raw pointers and smart pointers is generally a bad idea.

  5. 5.

    std::shared_ptr<> gained the ability to manage dynamic arrays already in C++17, but in C++17 you still had to initialize such pointers using their constructor and the new[] operator.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2020 Ivor Horton and Peter Van Weert

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Horton, I., Van Weert, P. (2020). Pointers and References. In: Beginning C++20. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5884-2_6

Download citation

Publish with us

Policies and ethics