piaoxusi0005 2022-11-19 11:57 采纳率: 85%
浏览 134
已结题

malloc.h文件找不到malloc_usable_size

我要获取我分配的动态内存大小,我开始用的sizeof(p)无论分配多大,获取的值一直是4,我想可能不对。
查看网上很多回答说可以用malloc_usable_size(p)获取
但我引入头文件#include <malloc.h>,运行代码提示找不到malloc_usable_size() , C/c++都尝试了,一样的错误



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

int main()
{
    char *p = malloc(100);
    char *h = p - 8;
    printf("%d\n", malloc_usable_size(p));

    return 0;
}

img


截图是我手动去查看malloc.h 文件确实没有找到 malloc_usable_size方法
错误 LNK2019 无法解析的外部符号 _malloc_usable_size,该符号在函数 _main 中被引用 CLearn E:\code_data\vs\c_learn\CLearn\CLearn\main.obj

  • 写回答

3条回答 默认 最新

  • 赵4老师 2022-11-19 12:59
    关注

    _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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月19日
  • 已采纳回答 11月19日
  • 创建了问题 11月19日