Intel® C++ Compiler Classic Developer Guide and Reference

ID 767249
Date 12/16/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Checking for Dangling Pointers

The pointer checker is not supported on macOS systems.

When dangling pointer checking or heap is enabled, the compiler uses a wrapper for the C runtime function free() and the C++ delete operator. These wrappers find all pointers that point to the block being freed, and change their bounds so that any access through the pointer will cause a bound violation. The bounds of these dangling pointers are actually set to:

  • lower_bound(p) = 2;

  • upper_bound(p) = 0;

If your program gets a bound violation with these bounds, it is the result of a reference through a dangling pointer.

When dangling pointer checking is enabled for stack, the compiler finds all pointers that point to the locals of the function and changes their bounds in the same way as heap pointers above, just before the function exits.

If you have a custom memory allocator, you can enable it to do dangling pointer checking. The free() function of your custom memory allocator should call this function in the pointer checker runtime code:

void __chkp_invalidate_dangling(void *ptr, size_t size);

This function is declared in the chkp.h file. You must include that header file to use this function because it uses a custom call interface.

Example

#include <chkp.h>
	 void my_free(void *ptr) {	
	  size_t size = my_get_size(ptr);	 
	 // do the free	 
 __chkp_invalidate_dangling(ptr, size);	
	 }

You can also enabled dangling pointer checking in any function you use to override the C++ delete operator.