Pointers/Memory Allocation   «Prev  Next»
Lesson 1

Pointers and Memory Allocation in C++

This module explores how C++'s use of pointers and memory allocation differs from C.
While pointers are used in much the same way in both languages, C++ introduces some interesting new features. In addition, C++ allows you to control the allocation and deallocation of a system-provided memory pool. This feature is particularly important for using dynamic data structures such as lists and trees.
Question: Which language features of C++ allow you to control the allocation and deallocation of a system-provided memory pool[1]?
As a C++ systems programmer, there are several powerful language features and libraries you can use to control the allocation and deallocation of a system-provided memory pool.
  1. Overloading New and Delete Operators: C++ allows you to overload the new and delete operators, both globally and at class level. Overloading these operators allows you to control the behavior of memory allocation and deallocation for objects. This is particularly useful when you want to allocate memory from a predefined memory pool instead of the default heap. Here is a simple example:
    class MyClass {
    public:
        void* operator new(std::size_t size) {
            // Allocate from custom memory pool here
        }
    
        void operator delete(void* pointer) {
            // Deallocate from custom memory pool here
        }
    };
    
  2. Custom Allocators: The C++ Standard Library makes heavy use of allocators, which are classes that encapsulate memory allocation and deallocation. By writing a custom allocator, you can control the source of the memory that library containers (like std::vector or std::list) use for storage. Allocators must implement a specific interface, which includes methods for memory allocation, deallocation, and addressing.
    template <class T>
    class MyAllocator {
        public:
            // typedefs required by allocator interface
            using value_type = T;
    
            // allocate memory
            T* allocate(std::size_t n) {
                // Allocate from custom memory pool here
            }
    
            // deallocate memory
            void deallocate(T* p, std::size_t n) {
                // Deallocate from custom memory pool here
            }
    };
    
  3. Placement New: C++ also includes a feature called "placement new", which allows you to construct an object at a particular memory location. This can be combined with a custom memory pool to control object allocation.
    void* myMemoryPool = ... ;  // Acquire block of memory from the memory pool
    MyClass* object = new(myMemoryPool) MyClass();  // Construct object in memory pool
    

Keep in mind that managing your own memory can lead to errors if not done carefully. You need to ensure that all memory is properly deallocated and that you handle allocation failures correctly. Memory management errors can lead to crashes, memory leaks, and other hard-to-diagnose issues.


Idea of a Pointer

C provides a remarkably useful type of variable called a pointer. A pointer is a variable that stores an address and its value is the address of another location in memory that can contain a value. You already used an address when you used the
  1. scanf() and
  2. scanf_s()
functions. A pointer variable with the name pNumber is defined by the second of the following two statements:
int Number = 25;
int *pNumber = &Number;

You declare a variable, Number, with the value 25, and a pointer, pNumber, which contains the address of Number. You can now use the variable pNumber in the expression *pNumber to obtain the value contained in Number. The * is the dereference operator, and its effect is to access the data stored at the address specified by a pointer.

This module discusses:
  1. How to create const pointer arguments to functions
  2. How to create aliases for variables using reference declarations
  3. How C++ implements call-by-reference using reference declaration
  4. How to use a generic pointer type
  5. How to use new and delete to manipulate free store memory
  6. How to create dynamically allocated multidimensional arrays
At the end of the module, you will be given the opportunity to take a quiz covering these topics.

[1]system-provided memory pool: In C++, a system-provided memory pool is a specialized mechanism offered by the underlying operating system or runtime environment to allocate and manage memory more efficiently than the standard malloc/free functions. While C++ itself doesn't have a built-in memory pool implementation, some operating systems and real-time environments often provide them as part of their standard library or specific system calls.

SEMrush Software