Witam
chcialbym wykorzystac funkcje z biblioteki CRT ale zastanawiam sie nad sensem z ponizszego powodu (przyklad z MSDN):
#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);
}
wszystko pieknie ale jesli wywolam free przed _CrtIsValidHeapPointer tak jak ponizej to w dalszym ciagu funkcja _CrtIsValidHeapPointer zwraca TRUE, czyli wskaznik jest poprawny:
#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");
free(my_pointer);
// 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");
}
jesli wywolam sobie dla tego przykladu:
_CrtIsValidPointer((const void *)my_pointer, sizeof(char) * 100000, TRUE) ;
to mi zwroci FALSE
jak wywolam:
_CrtIsValidPointer((const void *)my_pointer, sizeof(char) * 100, TRUE) ;
to mi zwroci TRUE
z innych przykladow jak napisze sobie np tak:
int* d = new int[4] ;
delete [] d ;
d[2] = 5 ;
_ASSERTE(_CrtCheckMemory());
to _CrtCheckMemory() zwraca TRUE, dlaczego?
moze mi ktos wyjasnic o co chodzi z tymi funkcjami, jak poprawnie je wykorzystac?