的malloc(0) [英] malloc(0)

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

问题描述

大家好,


malloc(0)的意义是什么?

它没有返回NULL指针,它返回了什么?

哪里可以使用这样的malloced指针?


感谢您的帮助。


问候,

Shailendra

Hi all,

What is the significance of "malloc(0)"?
It doesn''t return a NULL pointer, then what does it return?
Where one can use such a "malloced" pointer?

Thanks for help.

Regards,
Shailendra

推荐答案

" Mehta Shailendrakumar" < SH ******************* @ de.bosch.com>写道:
"Mehta Shailendrakumar" <sh*******************@de.bosch.com> wrote:
malloc(0)的意义是什么?


几乎没有。

它没有返回NULL指针,那么它返回什么?


在C89中,一个独特的指针;在C99中,好像大小是非零值
值,除了返回的指针不得用于访问

对象。在任何情况下指针都不是很有用;你不能取消引用

它,它的值和随机一样好。

哪里可以使用这样的malloced指针?
What is the significance of "malloc(0)"?
Nearly nothing.
It doesn''t return a NULL pointer, then what does it return?
In C89, "a unique pointer"; in C99, "as if the size were some nonzero
value, except that the returned pointer shall not be used to access an
object." In neither case is the pointer much use; you can''t dereference
it, and its value is as good as random.
Where one can use such a "malloced" pointer?




无处。好吧,人们可以将它传递给free()而不用担心这会导致问题。

唯一有用的方法就是当你不说话时关于

malloc(0)本身,但关于realloc(0)。这是一个可能的边缘情况

自动分配函数(例如,用于字符串处理),并且在

这样的情况下它是有用的,它不会导致未定义的行为。

那是AFAICT的全部。


Richard



Nowhere. Well, one can pass it to free() without worrying that this will
cause problems.
The only way in which this is useful is when you''re not talking about
malloc(0) itself, but about realloc(0). That is a possible marginal case
in automatic allocation functions (e.g., for string handling), and in
such situations it''s useful that it doesn''t cause undefined behaviour.
That, AFAICT, is all.

Richard


周一,20日2005年6月10:39:09 +0200,Mehta Shailendrakumar写道:
On Mon, 20 Jun 2005 10:39:09 +0200, Mehta Shailendrakumar wrote:
大家好,

malloc(0)的意义是什么? ?


什么都不好,malloc()碰巧允许0作为有效参数。

它没有返回NULL指针,那么它返回什么?


它可以返回一个空指针,对malloc()的任何调用都可以返回一个null

指针。但是一些实现总是为malloc(0)

返回null,而不是其他大小。

哪里可以使用这样的malloced指针?
Hi all,

What is the significance of "malloc(0)"?
Nothing great, malloc() happens to allow 0 as a valid argument.
It doesn''t return a NULL pointer, then what does it return?
It can return a null pointer, any call to malloc() can return a null
pointer. However some implementations always return null for malloc(0)
where they don''t for other sizes.
Where one can use such a "malloced" pointer?




它可以传递给realloc()和free()。它也可以与

其他指针进行比较,它必须比较不等于其他指向对象的指针

值和null。不幸的是,因为即使在正常情况下,你也不能保证获得malloc(0)的非空$返回值。条件你在哪里
会期望malloc()返回非null这不是便携式应用程序。


劳伦斯



It can be passed to realloc() and free(). It can also be compared against
other pointers where it must compare unequal to other pointer to object
values and null. Unfortunately since you aren''t guaranteed to get a
non-null return for malloc(0) even under "normal" conditions where you
would expect malloc() to return non-null this isn''t a portable application.

Lawrence




20/06/2005 10:56,Richard Bos写道:

On 20/06/2005 10:56, Richard Bos wrote:
" Mehta Shailendrakumar" < SH ******************* @ de.bosch.com>写道:
"Mehta Shailendrakumar" <sh*******************@de.bosch.com> wrote:
malloc(0)的意义是什么?
What is the significance of "malloc(0)"?



几乎没有。



Nearly nothing.

它没有返回NULL指针,那么它返回什么?
It doesn''t return a NULL pointer, then what does it return?



在C89中,一个唯一的指针;在C99中,好像大小是一些非零值,除了返回的指针不应用于访问
对象。在任何情况下指针都不是很有用;你不能取消引用它,它的价值和随机一样好。



In C89, "a unique pointer"; in C99, "as if the size were some nonzero
value, except that the returned pointer shall not be used to access an
object." In neither case is the pointer much use; you can''t dereference
it, and its value is as good as random.

在哪里可以使用这样的malloced。指针?
Where one can use such a "malloced" pointer?



无处。好吧,人们可以将它传递给free(),而不必担心这会导致问题。
这个有用的唯一方法就是当你不谈论
malloc时(0 )本身,但关于realloc(0)。这是自动分配函数中可能的边缘情况(例如,对于字符串处理),并且在这种情况下,它不会导致未定义的行为是有用的。
,AFAICT,就是全部。



Nowhere. Well, one can pass it to free() without worrying that this will
cause problems.
The only way in which this is useful is when you''re not talking about
malloc(0) itself, but about realloc(0). That is a possible marginal case
in automatic allocation functions (e.g., for string handling), and in
such situations it''s useful that it doesn''t cause undefined behaviour.
That, AFAICT, is all.




我想它至少可以与realloc一起使用。



I suppose it can at least be used with realloc.


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

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