查看指针指向的空间大小

_msize
Returns the size of a memory block allocated in the heap.

size_t _msize( void *memblock );

Routine Required Header Compatibility 
_msize <malloc.h> Win 95, Win NT 


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version 
LIBCMT.LIB Multithread static library, retail version 
MSVCRT.LIB Import library for MSVCRT.DLL, retail version 


Return Value

_msize returns the size (in bytes) as an unsigned integer.

Parameter

memblock

Pointer to memory block

Remarks

The _msize function returns the size, in bytes, of the memory block allocated by a call to calloc, malloc, or realloc.

When the application is linked with a debug version of the C run-time libraries, _msize resolves to _msize_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support.

Example

/* REALLOC.C: This program allocates a block of memory for
 * buffer and then uses _msize to display the size of that
 * block. Next, it uses realloc to expand the amount of
 * memory used by buffer and then calls _msize again to
 * display the new amount of memory allocated to buffer.
 */

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

void main( void )
{
   long *buffer;
   size_t size;

   if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
      exit( 1 );

   size = _msize( buffer );
   printf( "Size of block after malloc of 1000 longs: %u\n", size );

   /* Reallocate and show new size: */
   if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) 
        ==  NULL )
      exit( 1 );
   size = _msize( buffer );
   printf( "Size of block after realloc of 1000 more longs: %u\n", 
            size );

   free( buffer );
   exit( 0 );
}


Output

Size of block after malloc of 1000 longs: 4000
Size of block after realloc of 1000 more longs: 8000


Memory Allocation Routines

See Also   calloc, _expand, malloc, realloc

猜你喜欢

转载自blog.csdn.net/baidu_31437863/article/details/80427725