为size_t主场迎战使用intptr_t [英] size_t vs. intptr_t
问题描述
C标准保证了为size_t
是一个可以持有任何数组索引类型。这意味着,从逻辑上讲,为size_t
应该可以持有任何指针类型。我读过一些网站,我在谷歌发现,这是合法的和/或应始终工作:
无效* V =的malloc(10);
为size_t S =(为size_t)V;
于是在C99,该标准引入了使用intptr_t
和 uintptr_t形式
类型,这是符号和无符号类型保证要能容纳指针:
uintptr_t形式P =(为size_t)V;
那么究竟是什么两者的区别:为size_t
和 uintptr_t形式
?两者都是无符号的,都应该能够举办任何指针类型,因此他们似乎功能上完全相同。是否有任何真正令人信服的理由使用 uintptr_t形式
(或更好,但一个无效*
)而不是为size_t
,比其他的清晰度?在一个不透明的结构,其中区域将仅由内部功能来处理,没有任何理由不这样做呢?
同样, ptrdiff_t的
一直能够保持指针的差异,因此能够容纳几乎所有的指针已签名的类型,所以它是如何不同于使用intptr_t
?
是不是所有这些类型的服务基本上平凡的不同版本的同一功能的呢?如果不是,为什么?我可以用其中的一个,我不能与其他做不?如果是这样,为什么C99中添加两个本质上多余类型的语言?
我愿意不顾函数指针,因为他们并不适用于当前的问题,但随时提他们,因为我有一个偷渡怀疑他们将中央的正确的答案。
为size_t是可以持有任何数组索引类型。这意味着,
逻辑上,为size_t应该能够
持有任何指针类型
块引用>不一定!重提例如分段16位架构的日子:一个数组可能仅限于单个段(所以16位
为size_t
会做),但你可以有多段(所以32位使用intptr_t
键入将需要挑段以及其中的偏移量)。我知道这些事情在统一寻址不分段的架构,这些天听起来不可思议,但标准必须满足比什么是在2009年正常,你知道一个更广泛的 - - !)The C standard guarantees that
size_t
is a type that can hold any array index. This means that, logically,size_t
should be able to hold any pointer type. I've read on some sites that I found on the Googles that this is legal and/or should always work:void *v = malloc(10); size_t s = (size_t) v;
So then in C99, the standard introduced the
intptr_t
anduintptr_t
types, which are signed and unsigned types guaranteed to be able to hold pointers:uintptr_t p = (size_t) v;
So what is the difference between using
size_t
anduintptr_t
? Both are unsigned, and both should be able to hold any pointer type, so they seem functionally identical. Is there any real compelling reason to useuintptr_t
(or better yet, avoid *
) rather than asize_t
, other than clarity? In an opaque structure, where the field will be handled only by internal functions, is there any reason not to do this?By the same token,
ptrdiff_t
has been a signed type capable of holding pointer differences, and therefore capable of holding most any pointer, so how is it distinct fromintptr_t
?Aren't all of these types basically serving trivially different versions of the same function? If not, why? What can't I do with one of them that I can't do with another? If so, why did C99 add two essentially superfluous types to the language?
I'm willing to disregard function pointers, as they don't apply to the current problem, but feel free to mention them, as I have a sneaking suspicion they will be central to the "correct" answer.
解决方案size_t is a type that can hold any array index. This means that, logically, size_t should be able to hold any pointer type
Not necessarily! Hark back to the days of segmented 16-bit architectures for example: an array might be limited to a single segment (so a 16-bit
size_t
would do) BUT you could have multiple segments (so a 32-bitintptr_t
type would be needed to pick the segment as well as the offset within it). I know these things sound weird in these days of uniformly addressable unsegmented architectures, but the standard MUST cater for a wider variety than "what's normal in 2009", you know!-)这篇关于为size_t主场迎战使用intptr_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!