本文介绍了回顾我的回答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好clc。


以下是我对新闻组发布的问题的回复,

其中,一个人说我的建议错了,没有

说到哪里以及为什么。我转向clc。


[BEGINS]

Hi clc.

Following is my reply to a question posted in a newsgroup,
in which, a person said my advice was wrong without
saying where and why. I turn to clc.

[BEGINS]




这也是合法的,但是没有免费的行动(NULL)。

[结束]



This is also legal, but no action occurs for free ( NULL ).
[ENDS]

推荐答案




这是正确的。

*假设一些非零大小


这不是;或者更确切地说,这是误导。 malloc(0)允许返回

(在C89中)任何唯一指针;在C99中,它被允许表现得好像

它是一些非零大小_除了指针一定不能是
dereferenced_。当_just_假设

某些非零大小时,该限制不存在。

在任何一种情况下,使用返回值malloc()都可能导致未定义的行为。



This is correct.
* Assume some non-zero size
This is not; or rather, it is misleading. malloc(0) is allowed to return
(in C89) just any unique pointer; in C99, it is allowed to behave as if
it were some non-zero size _except that the pointer must not be
dereferenced_. That restriction is not present when you _just_ assume
some non-zero size.
In either case, using the return value malloc() can cause undefined
behaviour.




嗯,不。不完全的。使用它_as如果它指向一些可用的对象_

会导致未定义的行为。

在C89中,如果得到非null结果,我不确定是否,例如,

将它与任何其他相同类型或空指针进行相等比较

调用UB;我不能确定它是否应该是一个_valid_

唯一指针,如果它不是,那么即使是比较也是UB。如果是,

比较必须有效。

在C99中,如果得到非null结果,那么结果必须是有效的

指针,并且可以被视为任何指针_except_,它不能被取消引用
。例如,你可以比较它的相等性,你可以将它传递给一个函数(可能不会解析它,但是......),你可以分配

它是一个指针变量。如果你得到一个空指针,这两个标准也是如此。



Well, no. Not quite. Using it _as if it pointed to some usable object_
causes undefined behaviour.
In C89, if you get a non-null result, I''m not sure whether, e.g.,
comparing it for equality with any other same-type or null pointer
invokes UB; I can''t determine whether it is supposed to be a _valid_
unique pointer, and if it isn''t, even the comparison is UB. If it is,
the comparison must work.
In C99, if you get a non-null result, that result must be a valid
pointer, and can be treated as any pointer _except_ that it cannot be
dereferenced. For example, you can compare it for equality, you can pass
it to a function (which may then not deref it, but...), you can assign
it to a pointer variable. This is also true if you get a null pointer,
under either Standard.



这没关系。



This is OK.




当然,虽然没用,但我认为这不是

代码。



Albeit useless, of course, but I presume that was not the point of the
code.



如果相应的mallocs成功,那就没问题。



If the corresponding mallocs were successful, then this is OK.




即使不是,这也没关系。一个不成功的malloc()返回一个null

指针,而free(0)在两个标准下都是合法的(和一个no-op)。


Richard



Even if not, this is OK. An unsuccessful malloc() returns a null
pointer, and free(0) is legal (and a no-op) under both Standards.

Richard





这是正确的。

*假设一些非零大小


这不是;或者更确切地说,这是误导。 malloc(0)允许返回

(在C89中)任何唯一指针;在C99中,它被允许表现得好像

它是一些非零大小_除了指针一定不能是
dereferenced_。当_just_假设

某些非零大小时,该限制不存在。

在任何一种情况下,使用返回值malloc()都可能导致未定义的行为。



This is correct.
* Assume some non-zero size
This is not; or rather, it is misleading. malloc(0) is allowed to return
(in C89) just any unique pointer; in C99, it is allowed to behave as if
it were some non-zero size _except that the pointer must not be
dereferenced_. That restriction is not present when you _just_ assume
some non-zero size.
In either case, using the return value malloc() can cause undefined
behaviour.




嗯,不。不完全的。使用它_as如果它指向一些可用的对象_

会导致未定义的行为。

在C89中,如果得到非null结果,我不确定是否,例如,

将它与任何其他相同类型或空指针进行相等比较

调用UB;我不能确定它是否应该是一个_valid_

唯一指针,如果它不是,那么即使是比较也是UB。如果是,

比较必须有效。

在C99中,如果得到非null结果,那么结果必须是有效的

指针,并且可以被视为任何指针_except_,它不能被取消引用
。例如,你可以比较它的相等性,你可以将它传递给一个函数(可能不会解析它,但是......),你可以分配

它是一个指针变量。如果你得到一个空指针,这两个标准也是如此。



Well, no. Not quite. Using it _as if it pointed to some usable object_
causes undefined behaviour.
In C89, if you get a non-null result, I''m not sure whether, e.g.,
comparing it for equality with any other same-type or null pointer
invokes UB; I can''t determine whether it is supposed to be a _valid_
unique pointer, and if it isn''t, even the comparison is UB. If it is,
the comparison must work.
In C99, if you get a non-null result, that result must be a valid
pointer, and can be treated as any pointer _except_ that it cannot be
dereferenced. For example, you can compare it for equality, you can pass
it to a function (which may then not deref it, but...), you can assign
it to a pointer variable. This is also true if you get a null pointer,
under either Standard.



这没关系。



This is OK.




当然,虽然没用,但我认为这不是

代码。



Albeit useless, of course, but I presume that was not the point of the
code.



如果相应的mallocs成功,那就没问题。



If the corresponding mallocs were successful, then this is OK.




即使不是,这也没关系。一个不成功的malloc()返回一个null

指针,而free(0)在两个标准下都是合法的(和一个no-op)。


Richard



Even if not, this is OK. An unsuccessful malloc() returns a null
pointer, and free(0) is legal (and a no-op) under both Standards.

Richard




每个编译器都可以自由地定义malloc()的行为时
大小为0.通常,编译器记录它是如何做的。
两个可能性是:

当请求的大小为零时
*返回一个NULL指针。



Each compiler is free to define the behaviour of malloc () when
the size is 0. Usually, the compiler documents how it does.
Two possibilies are:

When the requested size is zero
* return a NULL pointer.



这是正确的。



This is correct.



这不是;或者更确切地说,这是误导。 malloc(0)被允许返回
(在C89中)任何唯一的指针;在C99中,允许它表现得好像它是一些非零的大小_除了指针不能被取消引用_。当_just_假设某些非零大小时,该限制不存在。



This is not; or rather, it is misleading. malloc(0) is allowed to return
(in C89) just any unique pointer; in C99, it is allowed to behave as if
it were some non-zero size _except that the pointer must not be
dereferenced_. That restriction is not present when you _just_ assume
some non-zero size.



嗯,不。不完全的。使用它_as如果它指向一些可用的对象_
会导致未定义的行为。
在C89中,如果你得到一个非null的结果,我不确定是否,比如
比较它与任何其他同类型或空指针相等
调用UB;我无法确定它是否应该是一个_valid_
唯一指针,如果它不是,那么即使比较是UB。如果是,则比较必须有效。
在C99中,如果得到非null结果,那么结果必须是有效的指针,并且可以被视为任何指针_except_ that它不能被取消引用。例如,你可以比较它是否相等,你可以将它传递给一个函数(它可能不会解析它,但是......),你可以将它指定给一个指针变量。如果在任一标准下获得空指针,也是如此。



Well, no. Not quite. Using it _as if it pointed to some usable object_
causes undefined behaviour.
In C89, if you get a non-null result, I''m not sure whether, e.g.,
comparing it for equality with any other same-type or null pointer
invokes UB; I can''t determine whether it is supposed to be a _valid_
unique pointer, and if it isn''t, even the comparison is UB. If it is,
the comparison must work.
In C99, if you get a non-null result, that result must be a valid
pointer, and can be treated as any pointer _except_ that it cannot be
dereferenced. For example, you can compare it for equality, you can pass
it to a function (which may then not deref it, but...), you can assign
it to a pointer variable. This is also true if you get a null pointer,
under either Standard.



这没关系。



This is OK.



虽然当然没用,但我认为这不是
代码的重点。



Albeit useless, of course, but I presume that was not the point of the
code.



如果相应的mallocs成功,那就没问题。



If the corresponding mallocs were successful, then this is OK.



即使没有,也没关系。一个不成功的malloc()返回一个null
指针,free和(0)在两个标准下都是合法的(和一个no-op)。

Richard



Even if not, this is OK. An unsuccessful malloc() returns a null
pointer, and free(0) is legal (and a no-op) under both Standards.

Richard




非常感谢你。这清除了我的怀疑。


Vijay



Thank you very much. This clears my doubt.

Vijay


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

08-13 13:38