调用大小为0的malloc [英] call to malloc with size 0

查看:86
本文介绍了调用大小为0的malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,


我在C中有一个与动态内存分配相关的简单问题,这是

代码:


#include< stdio.h>


int main()


{


char * p;


if((p =(char *)malloc(0))== NULL)


printf(" NULL \ n");


else


printf(" Valid Pointer \ n");


返回0;


}


输出:有效指针


为什么这段代码片段返回一个指向内存块的有效指针???


-Neo


Hi Folks,

I''ve a simple qestion related to dynamic memory allocation in C here is the
code:

#include <stdio.h>

int main()

{

char *p;

if( (p=(char*)malloc(0)) == NULL)

printf("NULL\n");

else

printf("Valid Pointer\n");

return 0;

}

Output : "Valid Pointer"

Why this code fragment returns a valid pointer to a memory block???

-Neo


推荐答案

在文章< 30 ************* @ uni-berlin.de>中, ti *************** @ yahoo.com

说...
In article <30*************@uni-berlin.de>, ti***************@yahoo.com
says...
为什么这段代码片段返回一个指向内存块的有效指针???

-Neo
Why this code fragment returns a valid pointer to a memory block???

-Neo




我之前见过这个。某些版本的UNIX将返回错误,

其他版本将返回有效指针。我不确定哪个是正确的但是我发现第二种情况更为常见且更有用,因为你不需要b
$我在某些代码中只有几个+ 1'只是为了
保证一个有效的指针,它恰好什么都没包含。这样我就知道指针已被初始化,即使它没有包含任何东西。

它的工作方式就像一面旗帜以及存放结果的地方。


-
http ://www.beluga.freeserve.co.uk




" Neo" < TI *************** @ yahoo.com>在消息中写道

新闻:30 ************* @ uni-berlin.de ...

"Neo" <ti***************@yahoo.com> wrote in message
news:30*************@uni-berlin.de...
嗨伙计,
我对C中的动态内存分配有一个简单的问题,这里是
代码:

#include< stdio.h>

int main()


char * p;

if((p =(char *)malloc(0))= = NULL)

printf(NULL \ n);



printf(" Valid Pointer\\\
) ;

返回0;

}
输出:有效指针

为什么此代码片段返回有效指向内存块的指针???

-Neo


看看标准是什么


4.10。 3内存管理功能:


如果请求的空间大小为零,则行为为

* implementation-defined *;返回的值应该是一个null

指针或一个唯一的指针。


请检查你的inslementation文件以获得答案!

- Ravi
Hi Folks,

I''ve a simple qestion related to dynamic memory allocation in C here is the code:

#include <stdio.h>

int main()

{

char *p;

if( (p=(char*)malloc(0)) == NULL)

printf("NULL\n");

else

printf("Valid Pointer\n");

return 0;

}

Output : "Valid Pointer"

Why this code fragment returns a valid pointer to a memory block???

-Neo

See what the standard says

4.10.3 Memory management functions:

If the size of the space requested is zero, the behavior is
*implementation-defined*; the value returned shall be either a null
pointer or a unique pointer.

So check in your impelementation documents for answer !

- Ravi



2004年11月17日星期三15:49:10 + 0530,Ravi Uday写道:
On Wed, 17 Nov 2004 15:49:10 +0530, Ravi Uday wrote:

Neo < TI *************** @ yahoo.com>在消息中写道
新闻:30 ************* @ uni-berlin.de ...

"Neo" <ti***************@yahoo.com> wrote in message
news:30*************@uni-berlin.de...
嗨伙计,
我在C中有一个与动态内存分配相关的简单问题
Hi Folks,

I''ve a simple qestion related to dynamic memory allocation in C here is


代码:

#include< stdio。 h>

int main()


char * p;

if((p =(char *)malloc(0))== NULL)


摆脱演员表。然后编译器应该抱怨,因为代码被破坏了,这是好的。添加演员阵容并没有修复

代码,它只是阻止编译器抱怨问题。


问题是你不是当你调用它时,在范围内有一个有效的malloc()

声明。在这种情况下,编译器假定malloc()返回int

,但它不会,它返回void *。所有

标准库函数都在

中提供了相应标准头文件的声明。对于malloc(),它是< stdlib.h>。

printf(" NULL \ n");



printf (有效指针\ n);

返回0;

}
输出:有效指针

为什么这段代码片段返回一个指向内存块的有效指针???


由于无效的强制转换,您的代码会调用未定义的行为,因此任何事情都可能发生。一旦修复了问题,如下所示,即编译器确实如此,因为标准说它可以。

-Neo
code:

#include <stdio.h>

int main()

{

char *p;

if( (p=(char*)malloc(0)) == NULL)
Get rid of the cast. The compiler should then complain, which is
good because the code is broken. Adding the cast doesn''t fix the
code, it just stops the compiler complaining about the problem.

The problem is that you don''t have a valid declaration for malloc()
in scope when you call it. In that case the compiler assumes
that malloc() returns int, but it doesn''t, it returns void *. All
standard library functions are provided with a declaration in
the appropriate standard header. For malloc() that is <stdlib.h>.

printf("NULL\n");

else

printf("Valid Pointer\n");

return 0;

}

Output : "Valid Pointer"

Why this code fragment returns a valid pointer to a memory block???
Your code invokes undefined behaviour due to the invalid cast, so anything
can happen. Once that''s fixed the issue is as below i.e. the compiler does
it because the standard says it can.

-Neo

<看看标准说的是什么

4.10.3内存管理功能:

如果请求的空间大小为零,行为是实现的*实现-defined *;返回的值应该是一个null指针或一个唯一的指针。

请检查你的inslementation文件以获得答案!


See what the standard says

4.10.3 Memory management functions:

If the size of the space requested is zero, the behavior is
*implementation-defined*; the value returned shall be either a null
pointer or a unique pointer.

So check in your impelementation documents for answer !



$ b $劳伦斯



Lawrence


这篇关于调用大小为0的malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆