Reader small image

You're reading from  Expert C++ - Second Edition

Product typeBook
Published inAug 2023
PublisherPackt
ISBN-139781804617830
Edition2nd Edition
Right arrow
Authors (5):
Marcelo Guerra Hahn
Marcelo Guerra Hahn
author image
Marcelo Guerra Hahn

Marcelo Guerra Hahn, With over 18 years of experience in software development and data analysis, Marcelo Guerra Hahn is a seasoned expert in C++, C#, and Azure. As an Engineering Manager at Microsoft C++ Team and former leader of SoundCommerce's engineering team, Marcelo's passion for data and informed decision-making shines through. He shares his knowledge as a lecturer at esteemed institutions like Lake Washington Institute of Technology and University of Washington. Through this book, Marcelo aims to empower readers with advanced C++ techniques, honed by real-world experience, to become proficient programmers and skilled data analysts.
Read more about Marcelo Guerra Hahn

Araks Tigranyan
Araks Tigranyan
author image
Araks Tigranyan

Araks Tigranyan is a passionate software engineer at Critical Techworks, with an unwavering love for the world of programming, particularly in C++. Her dedication to crafting efficient and innovative solutions reflects her genuine passion for coding. Committed to excellence and driven by curiosity, Araks continuously explores new technologies, going above and beyond to deliver exceptional work. Beyond programming, Araks finds solace in sports, with football holding a special place in her heart. As an author, Araks aspires to share her profound expertise in C++ and inspire readers to embark on their programming journeys.
Read more about Araks Tigranyan

John Asatryan
John Asatryan
author image
John Asatryan

John Asatryan, the Head of Code Republic Lab at Picsart Academy, seamlessly blends his academic background in International Economic Relations from the Armenian State University of Economics with his ventures in technology and education. Driven by a genuine passion for coding, John's commitment to empowering aspiring developers is evident in his expertise in the field. His unwavering dedication to bridging the gap between education and technology inspires others to pursue their coding dreams.
Read more about John Asatryan

Vardan Grigoryan
Vardan Grigoryan
author image
Vardan Grigoryan

Vardan Grigoryan is a senior backend engineer and C++ developer with more than 9 years of experience. Vardan started his career as a C++ developer and then moved to the world of server-side backend development. While being involved in designing scalable backend architectures, he always tries to incorporate the use of C++ in critical sections that require the fastest execution time. Vardan loves tackling computer systems and program structures on a deeper level. He believes that true excellence in programming can be achieved by means of a detailed analysis of existing solutions and by designing complex systems.
Read more about Vardan Grigoryan

Shunguang Wu
Shunguang Wu
author image
Shunguang Wu

Shunguang Wu is a senior professional staff at Johns Hopkins University Applied Physics Laboratory, and received his PhDs in theoretical physics and electrical engineering from Northwestern University (China) and Wright State University (USA), respectively. He published about 50 reviewed journal papers in the area of nonlinear dynamics, statistical signal processing and computer vision in his early career. His professional C++ experience started with teaching undergraduate courses in the late 1990s. Since then he has been designing and developing lots of R&D and end-user application software using C++ in world-class academic and industrial laboratories. These projects span both the Windows and Linux platforms.
Read more about Shunguang Wu

View More author details
Right arrow

Memory Management and Smart Pointers

Memory management can be defined as a process in which a computer’s memory is managed – for example, assigning memory to programs, variables, and more – so that it doesn’t affect the overall performance. Sometimes, the computer’s data can range up to terabytes, so efficiently using memory is necessary to minimize memory wastage and boost performance.

Memory management and smart pointers come at a price in C++. Programmers often complain about C++ because of its manual memory management requirements. While languages such as C# and Java use automatic memory management, it makes the programs run slower than their C++ counterparts. Manual memory management is often error-prone and unsafe. As we already saw in the previous chapters, a program represents data and instructions. Almost every program uses computer memory to some extent. It’s hard to imagine a useful program that doesn’t require memory allocation...

Technical requirements

Clang has support for some of the features of the C++ standard following C++20, informally referred to as C++2b. You can use Clang in C++2b mode with the -std=c++2b option, but you can use the g++ compiler with the -std=c++2a option to compile the examples throughout this chapter.

You can find the source files used in this chapter at https://github.com/PacktPublishing/Expert-C-2nd-edition.

Understanding computer memory

At the lowest level of representation, memory is a device that stores the state of a bit. Let’s say we are inventing a device that can store a single bit of information. Nowadays, it seems both meaningless and magical at the same time. It’s meaningless to invent something that was invented a long time ago. It’s magical because programmers nowadays have the luxury of stable multifunctional environments providing tons of libraries, frameworks, and tools to create programs without them even understanding them under the hood. It has become ridiculously easy to declare a variable or allocate dynamic memory, as shown in the following code snippet:

int x;double *pd = new double(3.14);

It’s hard to describe how the device stores these variables. To somehow shed some light on that magical process, let’s try to design a device that stores a bit of information.

Designing a memory storage device

We will use electrical...

Using smart pointers

Many languages support automated garbage collection. For example, memory acquired for an object is tracked by the runtime environment. It will deallocate the memory space after the object with a reference to it goes out of scope. Consider the following, for example:

// a code sample of the language (not-C++) supporting// automated garbage collection
void foo(int age) {
Person p = new Person("John", 35);
        if (age <= 0) { return; }
        if (age > 18) {
             p.setAge(18);
}
     // do something useful with the "p"
}
// no need to deallocate memory manually

In the preceding code block, the p reference (usually, references in garbage-collected languages are similar to pointers in C++) refers to the memory location returned by the new operator. The automatic...

Garbage collection

A garbage collector is a separate module that’s usually incorporated in the runtime environments of interpretable languages. For example, C# and Java both have garbage collectors, which makes programmers’ lives a lot easier. The garbage collector tracks all the object allocations in the code and deallocates them once they are not in use anymore. It’s called a garbage collector because it deletes the memory resource after it’s been used: it collects the garbage left by programmers.

It’s said that C++ programmers don’t leave garbage after them; that’s why the language doesn’t have support for a garbage collector. Though programmers tend to defend the language by stating that it doesn’t have a garbage collector because it’s a fast language, the truth is that it can survive without one.

Languages such as C# compile the program into an intermediate byte-code representation, which is then interpreted...

Using allocators

The idea behind an allocator is to provide control to container memory management. In simpler words, an allocator is an advanced garbage collector for C++ containers. Although we discuss allocators in the scope of container memory management, you can expand the idea to a generic garbage collector. At the beginning of this section, we implemented a badly designed garbage collector. When examining allocators, you will find a lot of similarities between the poorly designed GarbageCollector class and the default allocator in C++. Defined in <memory>, the default allocator has two basic functions – allocate() and deallocate(). The allocate() function is defined as follows:

   [[nodiscard]] constexpr T* allocate(std::size_t num);

The allocate() function acquires space for num objects of the T type. Pay attention to the [[nodiscard]] attribute – it means that the return value should not be discarded by the caller. The compiler will...

Summary

Garbage collectors in languages such as C# are provided by the environment. They work in parallel with the user program and try to clean up after the program whenever it seems efficient. We cannot do the same in C++; all we can do is implement a garbage collector directly in the program, providing a semi-automatic way of freeing the used memory resource. This mechanism is properly covered by the smart pointers that have been part of the language since C++11.

Memory management is one of the key components of every computer program. A program should be able to request memory dynamically during its execution. Good programmers understand the inner details of memory management. That helps them design and implement more performant applications. While manual memory management is considered an advantage, it tends to become painful in larger applications. In this chapter, we learned how we can avoid errors and handle memory deallocation using smart pointers. Having this basic understanding...

Questions

  1. From a high-level perspective, explain memory hierarchy.
  2. What is garbage collection and how does it work?
  3. Explain the different types of allocators.
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Expert C++ - Second Edition
Published in: Aug 2023Publisher: PacktISBN-13: 9781804617830
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.
undefined
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 $15.99/month. Cancel anytime

Authors (5)

author image
Marcelo Guerra Hahn

Marcelo Guerra Hahn, With over 18 years of experience in software development and data analysis, Marcelo Guerra Hahn is a seasoned expert in C++, C#, and Azure. As an Engineering Manager at Microsoft C++ Team and former leader of SoundCommerce's engineering team, Marcelo's passion for data and informed decision-making shines through. He shares his knowledge as a lecturer at esteemed institutions like Lake Washington Institute of Technology and University of Washington. Through this book, Marcelo aims to empower readers with advanced C++ techniques, honed by real-world experience, to become proficient programmers and skilled data analysts.
Read more about Marcelo Guerra Hahn

author image
Araks Tigranyan

Araks Tigranyan is a passionate software engineer at Critical Techworks, with an unwavering love for the world of programming, particularly in C++. Her dedication to crafting efficient and innovative solutions reflects her genuine passion for coding. Committed to excellence and driven by curiosity, Araks continuously explores new technologies, going above and beyond to deliver exceptional work. Beyond programming, Araks finds solace in sports, with football holding a special place in her heart. As an author, Araks aspires to share her profound expertise in C++ and inspire readers to embark on their programming journeys.
Read more about Araks Tigranyan

author image
John Asatryan

John Asatryan, the Head of Code Republic Lab at Picsart Academy, seamlessly blends his academic background in International Economic Relations from the Armenian State University of Economics with his ventures in technology and education. Driven by a genuine passion for coding, John's commitment to empowering aspiring developers is evident in his expertise in the field. His unwavering dedication to bridging the gap between education and technology inspires others to pursue their coding dreams.
Read more about John Asatryan

author image
Vardan Grigoryan

Vardan Grigoryan is a senior backend engineer and C++ developer with more than 9 years of experience. Vardan started his career as a C++ developer and then moved to the world of server-side backend development. While being involved in designing scalable backend architectures, he always tries to incorporate the use of C++ in critical sections that require the fastest execution time. Vardan loves tackling computer systems and program structures on a deeper level. He believes that true excellence in programming can be achieved by means of a detailed analysis of existing solutions and by designing complex systems.
Read more about Vardan Grigoryan

author image
Shunguang Wu

Shunguang Wu is a senior professional staff at Johns Hopkins University Applied Physics Laboratory, and received his PhDs in theoretical physics and electrical engineering from Northwestern University (China) and Wright State University (USA), respectively. He published about 50 reviewed journal papers in the area of nonlinear dynamics, statistical signal processing and computer vision in his early career. His professional C++ experience started with teaching undergraduate courses in the late 1990s. Since then he has been designing and developing lots of R&D and end-user application software using C++ in world-class academic and industrial laboratories. These projects span both the Windows and Linux platforms.
Read more about Shunguang Wu