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.
- 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
}
};
- 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
}
};
- 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
- scanf() and
- 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