Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Compares two memory states and returns their differences (debug version only).

int _CrtMemDifference( 
   _CrtMemState *stateDiff,
   const _CrtMemState *oldState,
   const _CrtMemState *newState 



Pointer to a _CrtMemState structure that is used to store the differences between the two memory states (returned).


Pointer to an earlier memory state (_CrtMemState structure).


Pointer to a later memory state (_CrtMemState structure).

If the memory states are significantly different, _CrtMemDifference returns TRUE. Otherwise, the function returns FALSE.

The _CrtMemDifference function compares oldState and newState and stores their differences in stateDiff, which can then be used by the application to detect memory leaks and other memory problems. When _DEBUG is not defined, calls to _CrtMemDifference are removed during preprocessing.

newState and oldState must each be a valid pointer to a _CrtMemState structure, defined in Crtdbg.h, that has been filled in by _CrtMemCheckpoint before calling _CrtMemDifference. stateDiff must be a pointer to a previously allocated instance of the _CrtMemState structure. If stateDiff, newState, or oldState is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, errno, _doserrno, _sys_errlist, and _sys_nerr is set to EINVAL and the function returns FALSE.

_CrtMemDifference compares the _CrtMemState field values of the blocks in oldState to those in newState and stores the result in stateDiff. When the number of allocated block types or total number of allocated blocks for each type differs between the two memory states, the states are said to be significantly different. The difference between the largest amount ever allocated at once for the two states and the difference between total allocations for the two states are also stored in stateDiff.

By default, internal C run-time blocks (_CRT_BLOCK) are not included in memory state operations. The _CrtSetDbgFlag function can be used to turn on the _CRTDBG_CHECK_CRT_DF bit of _crtDbgFlag to include these blocks in leak detection and other memory state operations. Freed memory blocks (_FREE_BLOCK) do not cause _CrtMemDifference to return TRUE.

For more information about heap state functions and the _CrtMemState structure, see Heap State Reporting Functions. For information about how memory blocks are allocated, initialized, and managed in the debug version of the base heap, see Memory Management and the Debug Heap.

Routine Required header Optional header Compatibility




Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For more compatibility information, see Compatibility in the Introduction.

Libraries: Debug versions of C Run-Time Libraries only.

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.

© 2015 Microsoft