Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
C++ Memory Management

You're reading from   C++ Memory Management Write leaner and safer C++ code using proven memory-management techniques

Arrow left icon
Product type Paperback
Published in Mar 2025
Publisher Packt
ISBN-13 9781805129806
Length 442 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Patrice Roy Patrice Roy
Author Profile Icon Patrice Roy
Patrice Roy
Arrow right icon
View More author details
Toc

Table of Contents (23) Chapters Close

Preface 1. Part 1: Memory in C++
2. Chapter 1: Objects, Pointers, and References FREE CHAPTER 3. Chapter 2: Things to Be Careful With 4. Chapter 3: Casts and cv-qualifications 5. Part 2: Implicit Memory Management Techniques
6. Chapter 4: Using Destructors 7. Chapter 5: Using Standard Smart Pointers 8. Chapter 6: Writing Smart Pointers 9. Part 3: Taking Control (of Memory Management Mechanisms)
10. Chapter 7: Overloading Memory Allocation Operators 11. Chapter 8: Writing a Naïve Leak Detector 12. Chapter 9: Atypical Allocation Mechanisms 13. Chapter 10: Arena-Based Memory Management and Other Optimizations 14. Chapter 11: Deferred Reclamation 15. Part 4: Writing Generic Containers (and a Bit More)
16. Chapter 12: Writing Generic Containers with Explicit Memory Management 17. Chapter 13: Writing Generic Containers with Implicit Memory Management 18. Chapter 14: Writing Generic Containers with Allocator Support 19. Chapter 15: Contemporary Issues 20. Chapter 16: Unlock Your Book’s Exclusive Benefits 21. Annexure: Things You Should Know 22. Index

Arrays

We have used arrays in our preceding examples, but we have not really provided a formal definition for that useful-yet-low-level construct. Note that in this section, the term “array” refers to raw, built-in arrays, not to other very useful but higher-level constructs such as std::vector<T> or std::array<T,N>.

Quite simply, in C++, an array is a contiguous sequence of elements of the same type. Thus, in the following excerpt, the a0 object occupies 10*sizeof(int) bytes in memory, whereas the a1 object occupies 20*sizeof(std::string) bytes:

int a0[10];
std::string a1[20];

The number of bytes between elements at indices i and i+1 in an array of some type T is precisely equal to sizeof(T).

Consider the following expression, which would be used in C++, as in C, for some array arr:

arr[i]

It evaluates to the same address as the following:

*(arr + i)

Since pointer arithmetic is typed, the + i part in this expression means “plus...

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
C++ Memory Management
You have been reading a chapter from
C++ Memory Management
Published in: Mar 2025
Publisher: Packt
ISBN-13: 9781805129806
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime
Modal Close icon
Modal Close icon