用C类型转换NULL指针 [英] Typecasting NULL pointer in C
问题描述
我有一个结构
struct detail {
int id;
uintptr_t init;
// blah blah
};
struct detail info;
info.id = 1;
info.init = (uintptr_t)NULL;
我要做出的的init
成员NULL。可能/可能不会发生什么,如果我类型转换(或不强制转换) NULL
?如果我直接给它NULL像 info.init = NULL;
这会让相对于运行时错误任何区别。它编译罚款。但是,code的执行是我最关心的。
I have to make the the init
member NULL. What may/may not happen if I typecast(or do not typecast) NULL
? What if I directly assign it NULL like info.init = NULL;
Does it make any difference with respect to runtime errors. It compiles fine. But the execution of the code is my main concern.
感谢
推荐答案
有在标准不能保证,如果 PTR
是一个空指针,则(uintptr_t形式)PTR
是 0
。
There is no guarantee in the standard that if ptr
is a null pointer, then (uintptr_t)ptr
is 0
.
如果你不关心哪个空指针和零整数是不等价的系统,则 info.init = 0;
的罚款。
If you don't care about systems on which null pointers and zero integers aren't equivalent, then info.init = 0;
is fine.
的的init
成员都有整数类型,它不能是造空。您可以指定 0
它,或者你可以给它分配一个空指针转换为 uintptr_t形式
的结果。在几乎每一个C实现过,这些都是同样的事情。但它不能保证,已经有系统的,它是不一样的。
The init
member has integer type, it cannot be "made null". You can assign 0
to it, or you can assign to it the result of converting a null pointer to uintptr_t
. On almost every C implementation ever, those are the same thing. But it is not guaranteed, and there have been systems on which it is not the same.
NULL
可能是一个空指针,或者它可能是一个整型常量 0
。在后一种情况下,是的标准保证,(uintptr_t形式)(NULL)
是 0
。因此,有可能实现在其 info.init = NULL; (无效*)(info.init);
是未定义行为。它不会导致空指针的整数相当于空是不为0,并计算了无效的指针的值是UB。
NULL
might be a null pointer, or it might be an integer constant 0
. In the latter case, there is a guarantee in the standard that (uintptr_t)(NULL)
is 0
. So there can be implementations on which info.init = NULL; (void*)(info.init);
has undefined behavior. It wouldn't result in a null pointer if the integer equivalent of null isn't 0, and computing an invalid pointer value is UB.
所以,如果你想保证信息
,当转换为指针类型,结果在一个空指针那么对于真正的便携性,你应该做的 info.init =(uintptr_t形式)(无效*)(NULL);
。你可以任意给读者一个额外的线索,通过包括在 uintptr_t形式
将被转换为指针类型,而不是无效*
。很少有很好的理由,用于存储 uintptr_t形式
,因此提示是怎么回事也许读者有所帮助。
So, if you want to guarantee that info
, when converted to a pointer type, results in a null pointer then for true portability you should do info.init = (uintptr_t)(void*)(NULL);
. You could optionally give the reader an extra clue by including the pointer type that the uintptr_t
is going to be converted to, instead of void*
. There are very few good reasons for storing a uintptr_t
, so hints what is going on might help the reader.
请注意,有在零值恒前pression,转换为指针类型标准的保证,是一个空指针。这确实的不的暗示零值不恒定的前pression,转换为指针类型,是一个空指针。它也不意味着空指针,转换为整数类型,0那些过去的两件事情是在大多数实现真正的(包括所有现代的)。
Note that there is a guarantee in the standard that a zero-valued constant expression, converted to pointer type, is a null pointer. This does not imply that a zero-valued non-constant expression, converted to a pointer type, is a null pointer. Neither does it imply that a null pointer, converted to integer type, is 0. Those last two things happen to be true in most implementations (including all "modern" ones).
这篇关于用C类型转换NULL指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!