size_t与uintptr_t [英] size_t vs. uintptr_t

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

问题描述

C标准保证size_t是可以容纳任何数组索引的类型.从逻辑上讲,这意味着size_t应该能够容纳任何指针类型.我在Google上发现的一些网站上读到,这是合法的,并且/或者应该一直有效:

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;

因此在C99中,该标准引入了intptr_tuintptr_t类型,它们是有符号和无符号类型,可以保证能够保存指针:

So then in C99, the standard introduced the intptr_t and uintptr_t types, which are signed and unsigned types guaranteed to be able to hold pointers:

uintptr_t p = (size_t) v;

那么使用size_tuintptr_t有什么区别?两者都是无符号的,并且都应该能够容纳任何指针类型,因此它们在功能上似乎相同.除了清晰度以外,还有什么真正令人信服的理由使用uintptr_t(或者更好的是void *)而不是size_t吗?在不透明的结构中,该字段仅由内部函数处理,是否有任何理由不这样做呢?

So what is the difference between using size_t and uintptr_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 use uintptr_t (or better yet, a void *) rather than a size_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?

通过相同的标记,ptrdiff_t是一种能够保留指针差异的有符号类型,因此能够容纳大多数指针,那么它与intptr_t有何区别?

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 from intptr_t?

不是所有这些类型都基本上为同一功能的不同版本提供服务吗?如果没有,为什么?我不能对其中一个进行处理,而对另一个却不能进行处理?如果是这样,为什么C99会在该语言中添加两种本质上多余的类型?

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是可以容纳任何数组索引的类型.这意味着, 从逻辑上讲,size_t应该能够 保留任何指针类型

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

不一定!追溯到分段16位体系结构的时代,例如:阵列可能仅限于单个段(因此16位size_t会做到)但您可能有多个段(因此32位intptr_t类型,以选择线段以及其中的偏移量).我知道在统一寻址的非分段体系结构的时代,这些事情听起来很奇怪,但是标准必须能够满足比"2009年的正常情况"更广泛的变化!-)

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-bit intptr_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与uintptr_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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