一个内存分配问题malloc

djvulee 2012-03-04 04:22:09

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

我的电脑上输出的是3.
谁能解释一下到底给p分配了多少内存吗?
...全文
185 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengcqw 2012-03-05
  • 打赏
  • 举报
回复
这是测试工程师常干的事[Quote=引用 10 楼 naletive 的回复:]
p = (char *)malloc(0)
第一次看到这个,有啥用?
[/Quote]
赵4老师 2012-03-05
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\malloc.c
/***
*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;
}
juso2011 2012-03-05
  • 打赏
  • 举报
回复
没有什么意义
salmon 2012-03-05
  • 打赏
  • 举报
回复
p = (char *)malloc(0)
第一次看到这个,有啥用?
huangziyun 2012-03-05
  • 打赏
  • 举报
回复
char *p;
p=(char *)malloc(sizeof(..));
if(!p)
{
printf("failed");
exit(1);
}

djvulee 2012-03-04
  • 打赏
  • 举报
回复
谢谢各位的回答,特别是感谢bluewanderer的回答!
AnYidan 2012-03-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 justkk 的回复:]
这个问题有什么意义?
[/Quote]

概念混乱!
bluewanderer 2012-03-04
  • 打赏
  • 举报
回复
C标准库没有获得malloc尺寸的方法,VC里realloc也是直接调用的HeapSize,所以这个无解。
soarfish 2012-03-04
  • 打赏
  • 举报
回复
strlen是p的长度,必须等待到'\0'才认为结束。
northcan 2012-03-04
  • 打赏
  • 举报
回复
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)返回值应该是随机的吧。
面包大师 2012-03-04
  • 打赏
  • 举报
回复
strlen(p),这个并不是去看p申请了多少内存,这个是字符串的长短,是从p的首地址开始知道遇到内存中出现字符结束符'\0'为止,结束的地方并不一定是输入p的内存。。。
justkk 2012-03-04
  • 打赏
  • 举报
回复
这个问题有什么意义?

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧