首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一个内存分配有关问题malloc

2012-04-06 
一个内存分配问题mallocC/C++ codeif( (p (char *)malloc(0)) NULL){puts(NULL)}else{//printf(%

一个内存分配问题malloc

C/C++ code
        if( (p = (char *)malloc(0)) == NULL)    {        puts("NULL");    }        else    {        //printf("%d\n",sizeof(p));        printf("%d\n",strlen(p));    }

我的电脑上输出的是3.
谁能解释一下到底给p分配了多少内存吗?

[解决办法]
strlen(p),这个并不是去看p申请了多少内存,这个是字符串的长短,是从p的首地址开始知道遇到内存中出现字符结束符'\0'为止,结束的地方并不一定是输入p的内存。。。
[解决办法]
size_t strlen( const char *string );
size_t wcslen( const wchar_t *string );

Return Values
Each of these functions returns the number of characters in string, excluding the terminal NULL. 

strlen函数返回一个字符串长度,strlen(p)应该没有什么意义,它是计算从p开始一直到遇到字符串结尾符'\0'的字符长度。而p地址之后内存中的内容未知,所以strlen(p)返回值应该是随机的吧。
[解决办法]
C标准库没有获得malloc尺寸的方法,VC里realloc也是直接调用的HeapSize,所以这个无解。
[解决办法]
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\malloc.c
C/C++ code
/****malloc.c - Get a block of memory from the heap**       Copyright (c) Microsoft Corporation. All rights reserved.**Purpose:*       Defines the malloc() function.********************************************************************************/#include <cruntime.h>#include <malloc.h>#include <internal.h>#include <mtdll.h>#include <dbgint.h>#include <rterr.h>#include <windows.h>#include <winheap.h>#include <rtcsup.h>extern int _newmode;    /* malloc new() handler mode */#ifdef _DEBUG#define _heap_alloc _heap_alloc_base#endif  /* _DEBUG *//****void *_heap_alloc_base(size_t size) - does actual allocation**Purpose:*       Same as malloc() except the new handler is not called.**Entry:*       See malloc**Exit:*       See malloc**Exceptions:********************************************************************************/__forceinline void * __cdecl _heap_alloc (size_t size){    if (_crtheap == 0) {        _FF_MSGBANNER();    /* write run-time error banner */        _NMSG_WRITE(_RT_CRT_NOTINIT);  /* write message */        __crtExitProcess(255);  /* normally _exit(255) */    }    return HeapAlloc(_crtheap, 0, size ? size : 1);}/****void *malloc(size_t size) - Get a block of memory from the heap**Purpose:*       Allocate of block of memory of at least size bytes from the heap and*       return a pointer to it.**       Calls the new appropriate new handler (if installed).**Entry:*       size_t size - size of block requested**Exit:*       Success:  Pointer to memory block*       Failure:  NULL (or some error value)**Uses:**Exceptions:********************************************************************************/void * __cdecl _malloc_base (size_t size){    void *res = NULL;    //  validate size    if (size <= _HEAP_MAXREQ) {        for (;;) {            //  allocate memory block            res = _heap_alloc(size);            //  if successful allocation, return pointer to memory            //  if new handling turned off altogether, return NULL            if (res != NULL)            {                break;            }            if (_newmode == 0)            {                errno = ENOMEM;                break;            }            //  call installed new handler            if (!_callnewh(size))                break;            //  new handler was successful -- try to allocate again        }    } else {        _callnewh(size);        errno = ENOMEM;        return NULL;    }    RTCCALLBACK(_RTC_Allocate_hook, (res, size, 0));    if (res == NULL)    {        errno = ENOMEM;    }    return res;} 


[解决办法]
这是测试工程师常干的事

探讨
p = (char *)malloc(0)
第一次看到这个,有啥用?

热点排行