Visual Studio 2010 C++: Get size of memory block allocated by malloc
问题 How can I get, given a pointer to a block of memory allocated with malloc, the size of it? For example: void* ptr = malloc( 10 ); //Allocate 10 bytes printf( "%d", GetMemSize( ptr ) ); //Should print 10 I want to do this for debugging purposes. 回答1: In Visual C++ you can use _msize() for that. 回答2: The Microsoft CRT has a function size_t _msize(void *memblock); which will give you the size of the allocated block. Note this may be (and in fact is likely to be) larger than the size asked for,