Share via


_CrtIsValidHeapPointer

Vérifie qu'un pointeur spécifié est dans le tas local (version Debug uniquement).

int _CrtIsValidHeapPointer(  
   const void *userData  
);

Paramètres

  • userData
    Pointeur vers le début d'un bloc de mémoire alloué.

Valeur de retour

_CrtIsValidHeapPointer renvoie TRUE si le pointeur spécifié est dans le tas local. Sinon, la fonction retourne FALSE.

Notes

La fonction _CrtIsValidHeapPointer est utilisée pour garantir qu'une adresse mémoire spécifique est dans le tas local. Le tas local fait référence au tas créé et géré par une instance particulière de la bibliothèque Runtime C. Si une bibliothèque de liens dynamiques (DLL) contient un lien statique vers la bibliothèque Runtime, elle possède sa propre instance du tas au moment de l'exécution, et par conséquent son propre tas, indépendamment du tas local de l'application. Lorsque _DEBUG n'est pas défini, Les appels de _CrtIsValidHeapPointer sont supprimés pendant le prétraitement.

Cette fonction renvoie TRUE ou FALSE, elle peut être passé à l'une des macros _ASSERT pour créer un mécanisme simple de gestion des erreurs d'erreur de débogage. L'exemple suivant provoque un échec d'assertion si l'adresse spécifiée ne se trouve pas dans le tas local :

_ASSERTE( _CrtIsValidHeapPointer( userData ) );

Pour plus d'informations sur la façon dont _CrtIsValidHeapPointer peut être utilisé avec d'autres fonctions et des macros de débogage, consultez Macros pour la création de rapports. Pour obtenir des informations sur la façon dont les blocs de mémoire sont alloués, initialisés, et gérés dans la version Debug du tas de base, consultez Détails du tas de débogage CRT.

Configuration requise

Routine

En-tête requis

_CrtIsValidHeapPointer

<crtdbg.h>

Pour plus d'informations sur la compatibilité, consultez Compatibilité dans l'introduction.

Bibliothèques

Seulement les versions debug des bibliothèques Runtime C.

Exemple

// crt_isvalid.c
/*
 * This program allocates a block of memory using _malloc_dbg
 * and then tests the validity of this memory by calling 
 * _CrtIsMemoryBlock,_CrtIsValidPointer, and _CrtIsValidHeapPointer.
 */

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <crtdbg.h>

#define  TRUE   1
#define  FALSE  0

int main( void )
{
        char *my_pointer;

        /* 
         * Call _malloc_dbg to include the filename and line number
         * of our allocation request in the header information
         */
        my_pointer = (char *)_malloc_dbg( sizeof(char) * 10, 
        _NORMAL_BLOCK, __FILE__, __LINE__ );

        // Ensure that the memory got allocated correctly
        _CrtIsMemoryBlock((const void *)my_pointer, sizeof(char) * 10, 
        NULL, NULL, NULL );

         // Test for read/write accessibility
        if (_CrtIsValidPointer((const void *)my_pointer, 
        sizeof(char) * 10, TRUE))
                printf("my_pointer has read and write accessibility.\n");
        else
                printf("my_pointer only has read access.\n");

        // Make sure my_pointer is within the local heap
        if (_CrtIsValidHeapPointer((const void *)my_pointer))
                printf("my_pointer is within the local heap.\n");
        else
                printf("my_pointer is not located within the local"
                       " heap.\n");

        free(my_pointer);
}

Sortie

my_pointer has read and write accessibility.
my_pointer is within the local heap.

Équivalent .NET Framework

Non applicable. Pour appeler la fonction C standard, utilisez PInvoke. Pour plus d'informations, consultez Exemples d'appel de plateforme.

Voir aussi

Référence

Routines de débogage