C++ is a large and complex language, but it gives programmers complete freedom when it comes to management of dynamic memory. This allows the programmers to allocate memory and manipulate it at runtime. That is why C++ is still a favorite language for high performance applications in various domains such as gaming, telecom, finance, aerospace, etc.
However, it requires programmers to take great care while using dynamic memory, such as releasing acquired memory, taking care not to overstep memory boundary, etc. Otherwise, it could lead to problems such as dangling pointers, memory corruption, memory leaks, etc.
C++ is a large and complex language, but it gives programmers complete freedom when it comes to management of dynamic memory. This allows the programmers to allocate memory and manipulate it at runtime. That is why C++ is still a favorite language for high performance applications in various domains such as gaming, telecom, finance, aerospace, etc.
However, it requires programmers to take great care while using dynamic memory, such as releasing acquired memory, taking care not to overstep memory boundary, etc. Otherwise, it could lead to problems such as dangling pointers, memory corruption, memory leaks, etc.
This course will help you overcome all these problem by leveraging the excellent features that Visual Studio provides. It has a rich set of functions provided by the C/C++ runtime heap library. These functions can help detect memory leaks, overflows, etc. You'll learn how to use these functions effectively and make your programs bug-free.
In this course, you'll start with the basics of heap memory management and understand C & C++ allocation functions/operators in depth. You'll also learn how to effectively use them to avoid memory problems. Afterwards, you'll learn about the Visual Studio heap library functions and understand how to use them in your code.
By the end of this course, you'll have a deep understanding of dynamic memory management. You'll be able to use CRT heap functions effectively to detect & isolate memory problems. You'll also implement the Visual Studio functions so that they can be used with any C++ compiler.
Note:This course requires Visual Studio 2017 or a higher version.
Slide deck
We'll discuss the basics of a process that represents the virtual address space of the program. You'll learn about the different sections in the address space and how they're used in a C/C++ program
This is a short introduction to pointers. You can skip this video if you're already familiar with pointers.
Here, I'll given a quick introduction to using Visual Studio. If you've never used it before, this video will help you understand its features and use it for creating C/C++ projects.
This lecture shows how to debug C/C++ application in Visual Studio. I'll show some important shortcuts that will make you more productive while debugging applications.
Throughout this course, we'll frequently view data inside the memory window of Visual Studio. However, to understand that, you have to know byte ordering which is explained in this lecture.
Gives an overview of the heap allocation functions in C, such as malloc, calloc, realloc & free.
This lecture shows how to use the C allocation functions with code example in Visual Studio.
In this lecture, you'll learn how to use realloc for increasing size of an existing memory block.
Understand & implement dynamic memory allocation in C++ using new & delete operators.
Learn the internals of new.
See how new internally works by examining the disassembly of C++ source code.
Understand the behavior of new when it fails.
Instead of throwing an exception, the new operator can call a user defined function, called as new handler. This video explains how to set the new handler.
We can suppress the throwing behavior of new through the nothrow constant. This video explains this with an example.
In this two part video, I explain why we might need to use the non-throwing new. The example demonstrates the issues with an executable and a dynamic linked library.
This video shows what can go wrong if exceptions are thrown across DLL boundaries, especially when executable and the DLL are compiled with different C++ compilers.
Project files for Lecture 15 & 16
Introduction to the most powerful new, the placement new.
Demonstration of how to use the placement new.
We'll learn how placement new helps avoid creation of temporary objects when memory is allocated for an array.
Another example that demonstrates the usage of placement new in increasing performance of the program when data is accessed.
(Check the attached material for the code of overloaded new & delete functions. Just paste the code in main source file to see calls to new & delete in console)
This video shows how placement new can be used to place frequently accessed objects together. This allows the OS to put small chunks of data together in the cache, enabling fast access to such data.
new & delete operators internally call the operator new & operator delete functions. Learning about these functions in this lesson.
Gives a brief description of common issue with memory management.
This video explains the problems that can arise due to uninitialized pointers and how to avoid them.
Continues with the example of uninitialized pointers with more use cases.
In this video, we'll examine buffer overflows in stack memory.
Here, I discuss buffer overflows in heap.
In this lecture, you'll learn the concept of dangling pointers.
Code example of a dangling pointer.
Memory leaks is a very common problem in C/C++ applications. You'll learn & understand what memory leak is.
We continue with the previous example and address the issue of memory leak.
Download the source code of String & HeapChecker classes
We'll start this section by creating String class that uses dynamic memory for storing strings. It will be used as a base example for understanding heap corruption & its resolution.
Implementation of Allocate & Assign member functions
Implementation of the Insert() member function.
The constructor & the Insert() functions have some problems that we'll fix in this lecture.
You'll learn how to use the _CrtCheckMemory() function to detect heap corruption.
To avoid the long & tedious process of finding the allocations that are corrupted, we'll create a helper class. This class will use the principle of RAII (resource acquisition is initialization) to detect heap corruptions.
We'll check heap corruptions in String class through our Heapchecker helper class.
We'll modify our class to display the output in the debugger windows (Visual Studio output window).
Redirecting to the debugger window requires our program to be executed through a debugger. This will slow down the program and the output also gets mixed up with other messages in the debugger window. Therefore, in this lecture, we'll add support for writing the messages into a file.
The log file gets opened multiple times if heap corruptions are detected. To avoid this, we'll modify our class that will open the file when the program starts and close it when the program terminates.
This lecture introduces the _CrtDumpMemory() function, that can detect and dump leaks in the debugger window.
We'll get to know this function better with an example.
CRT debug heap library provides flags that control the behavior of _Crt functions. I'll explain how to use some flags to enable automatic leak check when the program terminates, without having to call _CrtDumpMemoryLeaks() manually at every exit point.
For malloc(), a detailed leak dump is displayed automatically through _CRTDBG_MAP_ALLOC macro. For new, there is no such macro. In this lecture, we'll learn how new can generate detailed leak dumps just like malloc().
Assignment for detecting leaks in String class.
Leaks can also be detected by creating heap snapshots (or checkpoints) that can be compared later. Any difference between snapshots indicates a leak. This lecture will explain basics of memory checkpoints.
We'll implement the concepts learnt in the earlier video through an example in Visual Studio.
We'll again use RAII to create a helper class that will automatically create checkpoints and compare them.
We examine some issues while using checkpoints and also discuss how to avoid them.
Visual Studio provides a heap analyzer tool that can also be used for taking snapshots of the memory. In this lecture, I'll show you how to use it.
We'll use the heap analyzer in another example.
Source files for Reports lectures
Understand the concept of report mode & type. These can be used to redirect the output of _Crt function to debugger window, message box or a file.
We examine different report modes & types with a code example.
In this lecture, I explain how to use the file report mode.
Complete source code of the library to detect leaks & heap corruptions.
You'll learn the internals of leak detection mechanism of Visual Studio. We'll implement our leak detector using the same technique.
This lecture begins the implementation by creating a header called ptMemoryBlockHeader that will store information about the memory allocation that needs to be tracked. We'll also implement our own malloc-like function called ptmalloc that will use the header to store allocation information. Multiple allocations will form a linked-list of header blocks.
After implementing ptmalloc(), we'll implement the corresponding free function called ptfree(). This function will free the memory and remove the memory block from the linked list.
In this lecture, we'll create the PtDumpLeaks() function. This is just like _CrtDumpMemoryLeaks() provided by Visual Studio CRT.
Now, we need to add support for detecting leaks in C++ applications. For this, we've to overload different forms of new & delete. The overloads will just call the C functions internally. We'll see how to do that in this lecture and also overcome issues of mixing C & C++ code using extern "C" directive.
We'll finish off the C++ implementation and also optimize the code for Release builds.
Our library works on Windows, but we need to check it on Linux. Obviously, there would be some issues due to absence of certain macros in g++. We'll examine these issues and modify the code so that it can become platform independent.
In this lecture, I'll explain the internals of how heap corruption is detected by Visual Studio,
We'll modify the ptMemoryBlockHeader structure to account for extra space for no mans land. We'll also start the implementation of PtCheckMemory() function that will check for heap corruptions (just like _CrtCheckMemory() ).
We'll finish the implementation of PtCheckMemory() in this lecture.
CPU's like to have data aligned in the memory in multiples of their word sizes. If the data is misaligned, the CPU has to incur a heavy performance cost. Structures may have this issue, but the compilers take care by padding some bytes to existing values. In our implementation, this causes failure to while checking for heap corruptions before the memory block. In this two-part lecture, I explain why this happens.
In this lecture, we'll fix ptMemoryBlockHeader to ensure heap corruption detection works properly. I explain in depth by showing the padding and the surrounding data in the memory window.
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.