We may earn an affiliate commission when you visit our partners.
Umar Lone

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.

Read more

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.

Enroll now

What's inside

Learning objectives

  • Understand different kinds of problems associated with memory management
  • Effectively detect & isolate memory issues
  • Gain confidence in c/c++ memory management
  • Understand and use the facilities provided by visual studio for detecting memory issues

Syllabus

Understand virtual address space, pointers & how dynamic memory is allocated in C

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

Read more

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 how dynamic memory works in C++

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.

Understand different types of issues that can arise with improper memory management.

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.

Learn how to use Visual Studio to detect heap corruption in your C/C++ programs.

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.

You'll learn how to detect & isolate memory leaks in your C/C++ applications.

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.

Learn how to create a custom leak detector that will work on any platform with a standard C or C++ compiler.

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.

Understand how heap corruption mechanism works by adding this feature to the custom leak detector

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.

BONUS LECTURE

Good to know

Know what's good
, what to watch for
, and possible dealbreakers
Provides a deep dive into dynamic memory management, which is essential for writing robust and efficient C++ applications in resource-intensive domains
Teaches how to leverage Visual Studio's heap library functions, which can significantly aid in detecting and isolating memory-related bugs
Requires Visual Studio 2017 or higher, which may necessitate an upgrade for developers using older versions of the IDE
Explores the internals of memory allocation functions like `new` and `delete`, offering insights into low-level memory management
Covers techniques for creating a custom leak detector that works on any platform, which can be valuable for cross-platform development
Focuses on detecting memory leaks and heap corruption, which are common and challenging issues in C/C++ development

Save this course

Save Debugging C/C++ Memory Issues in Visual Studio to your list so you can find it easily later:
Save

Activities

Be better prepared before your course. Deepen your understanding during and after it. Supplement your coursework and achieve mastery of the topics covered in Debugging C/C++ Memory Issues in Visual Studio with these activities:
Review C/C++ Pointers
Solidify your understanding of pointers in C and C++ before diving into memory management. A strong grasp of pointers is crucial for understanding dynamic memory allocation and potential pitfalls.
Browse courses on Memory Management
Show steps
  • Review pointer arithmetic and syntax.
  • Practice declaring and dereferencing pointers.
  • Work through examples of pointers to pointers.
Read 'Hacking: The Art of Exploitation'
Learn about common memory-related security vulnerabilities. Understanding how memory can be exploited will help you write more secure and robust C/C++ code.
Show steps
  • Read the chapters on buffer overflows and stack smashing.
  • Experiment with the code examples in a safe environment.
  • Reflect on how these vulnerabilities can be prevented.
Read 'Effective C++' by Scott Meyers
Gain a deeper understanding of C++ best practices and common pitfalls. This book provides practical advice on writing effective and efficient C++ code, which is essential for avoiding memory-related bugs.
Show steps
  • Read and take notes on each item in the book.
  • Try to apply the advice to your own code.
  • Reflect on how the advice relates to memory management.
Four other activities
Expand to see all activities and additional details
Show all seven activities
Create a memory management cheat sheet
Consolidate your knowledge of memory management by creating a cheat sheet. This will help you quickly recall important concepts and techniques when debugging memory issues.
Show steps
  • Review the course materials and identify key concepts.
  • Organize the information into a concise and easy-to-read format.
  • Include code examples and diagrams to illustrate the concepts.
  • Share the cheat sheet with other students for feedback.
Write a blog post on memory leaks
Solidify your understanding of memory leaks by explaining the concept to others. Writing a blog post will force you to organize your thoughts and present the information in a clear and concise manner.
Show steps
  • Research different types of memory leaks.
  • Write a clear and concise explanation of memory leaks.
  • Include code examples to illustrate the concept.
  • Proofread and edit the blog post.
Implement a custom memory allocator
Reinforce your understanding of memory allocation by implementing your own allocator. This hands-on exercise will help you internalize the concepts of heap management and memory fragmentation.
Show steps
  • Design the allocator's data structures.
  • Implement allocation and deallocation functions.
  • Test the allocator with various allocation patterns.
  • Profile the allocator's performance.
Build a memory debugging tool
Apply your knowledge of memory management to create a practical tool. Building a memory debugger will give you hands-on experience with detecting and diagnosing memory issues.
Show steps
  • Design the architecture of the debugger.
  • Implement memory tracking and analysis features.
  • Add a user interface for visualizing memory usage.
  • Test the debugger with real-world applications.

Career center

Learners who complete Debugging C/C++ Memory Issues in Visual Studio will develop knowledge and skills that may be useful to these careers:
Software Engineer
A software engineer frequently needs to manage memory, particularly when working with C++. This course helps build a foundation by providing an in-depth look at dynamic memory management. It teaches how to use allocation functions and operators effectively, which is very relevant for a software engineer, and how to use Visual Studio's heap library to detect and isolate memory problems. The course also covers implementing Visual Studio functions for use with any C++ compiler. Anyone who wishes to be a software engineer and work with C++ should take this course as it may improve code quality and reduce debugging time.
Game Developer
Game developers often use C++ for its performance when managing memory. This course helps build a foundation by teaching how to handle dynamic memory using Visual Studio. A game developer can use this course to learn how to use allocation functions and operators, and effectively debug memory issues. The course also teaches the use of Visual Studio heap library functions, which can be critical for optimizing game code. This course also covers implementing Visual Studio functions for use with any C++ compiler. Game developers looking to leverage C++ will find this course especially helpful.
Embedded Systems Engineer
Embedded systems engineers often work with C and C++ where manual memory management is critical. This course is helpful, as it develops the necessary skills for managing dynamic memory allocations. An embedded systems engineer will see immediate improvements in their ability to use allocation functions and operators, and debug memory issues in Visual Studio. The course also covers implementation of the Visual Studio heap library functions, making them more versatile. An embedded systems engineer should take this course to improve the reliability of their embedded software.
Firmware Engineer
Firmware engineers require proficiency in C and C++ to manage system resources efficiently. This course helps build a foundation, particularly in handling dynamic memory through features offered by Visual Studio. A firmware engineer will find the skills gained through this course on using allocation functions and operators, along with the debugging techniques, to be invaluable. Additionally, this course covers implementation of Visual Studio heap library functions for greater versatility. Firmware engineers seeking to refine their C++ programming skills will find this course especially pertinent.
High-Performance Computing Engineer
A high performance computing engineer benefits from this course because it directly addresses memory optimization in C++. This course is helpful, as it focuses on using dynamic memory management and the debugging tools within Visual Studio to isolate and fix memory errors. A high performance computing engineer will benefit from the course's deep dive into C/C++ allocation functions and operators. The knowledge of Visual Studio heap library functions also contributes towards improved performance. Those working in high-performance environments and who use C++ should consider this course.
Robotics Engineer
Robotics engineers who use C++ can improve their skills through this course which focuses on memory management. This course may be useful, as it covers how dynamic memory is allocated and managed in C++. A robotics engineer will find the debugging techniques taught in Visual Studio to be a very useful skill to acquire. The course also covers the implementation of Visual Studio heap library functions for use with any C++ compiler. A robotics engineer may find this course beneficial for optimizing their software and to reduce memory related errors.
Quantitative Analyst
Quantitative analysts in finance sometimes use high performance C++ for their analyses. This course may be useful, as it imparts essential skills in memory management. A quantitative analyst would need to understand how dynamic memory is allocated and manipulated. The course also covers how to use Visual Studio's heap library to detect and isolate memory problems.The practical use of Visual Studio to implement functions with any C++ compiler is useful. A quantitative analyst working with C++ may find this course beneficial for developing reliable and efficient code.
Computer Graphics Programmer
A computer graphics programmer frequently works with C++, and can benefit from a deeper understanding of memory management. This course may be useful, as it provides hands on experience with debugging and managing memory allocations in Visual Studio. The course covers the use of allocation functions and operators, and effectively using the debugging tools. The course material related to the Visual Studio heap library functions may also contribute towards building robust applications. A computer graphics programmer who needs to optimize their software may find this course helpful.
Database Developer
Database developers who work with C++ can find this course helpful for managing memory effectively. This course may be useful, as it covers dynamic memory management and debugging tools within Visual Studio. A database developer may benefit from the course's focus on C/C++ allocation functions and operators. The course material about Visual Studio heap library functions, may help improve memory allocation. Database developers looking to optimize C++ applications may benefit from this course.
Operating Systems Developer
Operating systems developers often use C and C++, and this course may be useful for strengthening their memory management skills. This course delves into allocating and manipulating memory using Visual Studio, thereby helping the developer. It also goes into debugging memory issues, and helps the developer use Visual Studio's heap library functions. The course also includes material on implementing Visual Studio functions for use with any C++ compiler. An operating systems developer can use this course to improve their code and reduce errors.
Compiler Developer
Compiler developers who work with C++ may find this course useful for debugging and optimizing memory management. This course may be useful, as it will help them to understand the impact of different allocation strategies. As a memory management course, compiler developers might find the debugging and memory management techniques taught in Visual Studio useful. The course material covering the implementation of visual studio heap library may also help compiler developers gain a clearer picture about memory management. Compiler developers aiming to improve their understanding of memory allocation may find this course helpful.
Reverse Engineer
Reverse engineers sometimes use C and C++ to analyze software, and a deeper understanding of memory management may be helpful. In this course, reverse engineers may find Visual Studio's debugging tools and the material related to dynamic memory management useful. The course covers many memory management techniques and debugging issues using the Visual Studio heap library. A reverse engineer looking to deepen their technical analysis skills may find this course beneficial.
Security Analyst
Security analysts who analyze software in C++ may find this course to be useful for understanding memory vulnerabilities. This course may be useful, as it covers memory management techniques using Visual Studio, which may be helpful in identifying vulnerabilities in C++ code. The course also introduces many debugging techniques for C++ applications. Security analysts would benefit from the material on Visual Studio heap library functions. A security professional seeking to identify memory related vulnerabilities may find this course helpful.
Scientific Computing Programmer
Scientific computing programmers may find this course useful if their work involves C++ and requires manual memory management. This course may be useful, as it provides hands on experience in debugging and managing dynamic memory in Visual Studio. They can also learn how to use allocation functions and operators and the Visual Studio heap library functions effectively. A scientific computing programmer may find that this course helps improve their ability to handle complex simulations.
Technical Writer
Technical writers who document C++ software may find this course useful, as gaining familiarity with memory management techniques may improve the quality of documentation. This course may be useful, as it covers dynamic memory management, and the debugging process in Visual Studio. A technical writer can use this information to understand what a C++ program does. This course may also help a technical writer, by showing how C++ memory allocation works, and what steps might be performed to find errors. A technical writer looking to document C++ projects may find this course helpful.

Reading list

We've selected two books that we think will supplement your learning. Use these to develop background knowledge, enrich your coursework, and gain a deeper understanding of the topics covered in Debugging C/C++ Memory Issues in Visual Studio.
Provides invaluable insights into writing robust and efficient C++ code. It covers many common pitfalls and best practices related to memory management, object lifetime, and resource allocation. It is particularly useful for understanding the nuances of C++ and avoiding subtle errors that can lead to memory issues. This book is commonly used by industry professionals.
Provides a deep dive into low-level programming and security vulnerabilities. While not solely focused on memory management, it covers buffer overflows, stack smashing, and other memory-related exploits in detail. Understanding these vulnerabilities is crucial for writing secure and robust C/C++ code. This book is more valuable as additional reading than it is as a current reference.

Share

Help others find this course page by sharing it with your friends and followers:

Similar courses

Similar courses are unavailable at this time. Please try again later.
Our mission

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.

Affiliate disclosure

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.

© 2016 - 2025 OpenCourser