TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Static Integer Types (2021)

34 点作者 isaacaggrey将近 3 年前

6 条评论

tialaramex将近 3 年前
Note that, here in 2022, those ARM chips with CHERI do now exist, Morello: <a href="https:&#x2F;&#x2F;www.arm.com&#x2F;architecture&#x2F;cpu&#x2F;morello" rel="nofollow">https:&#x2F;&#x2F;www.arm.com&#x2F;architecture&#x2F;cpu&#x2F;morello</a><p>Also, although this article says Rust has to choose usize ~= u128 on Morello which is unpalatable, Aria proposes that instead Rust <i>tweaks the definition of usize</i> to say it&#x27;s about addresses not pointers, and thus usize ~= u64 on Morello.<p><a href="https:&#x2F;&#x2F;gankra.github.io&#x2F;blah&#x2F;fix-rust-pointers&#x2F;" rel="nofollow">https:&#x2F;&#x2F;gankra.github.io&#x2F;blah&#x2F;fix-rust-pointers&#x2F;</a> leading into <a href="https:&#x2F;&#x2F;gankra.github.io&#x2F;blah&#x2F;tower-of-weakenings&#x2F;" rel="nofollow">https:&#x2F;&#x2F;gankra.github.io&#x2F;blah&#x2F;tower-of-weakenings&#x2F;</a><p>If you have nightly Rust, you can play with Aria&#x27;s new semantics because she implemented them. I think they&#x27;re a good idea, but I don&#x27;t have much &quot;skin in the game&quot;, unlike, apparently the author of this article.
wahern将近 3 年前
&gt; I hope it’s uncontroversial that, like Rust, languages should not allow implicit casts between integer types at all.<p>I find this controversial. The unstated option #4 for addressing C&#x27;s permissive implicit narrowing conversions is to simply disallow implicit narrowing conversions, but continue providing implicit integer conversions to types of greater-than-or-equal rank.<p>I suspect the reason you left out option #4 is an entirely different, self-imposed constraint in Rust:<p>&gt; [Rust] defines From casts for integer types that will succeed on every platform. Since casting a 32-bit integer to a usize would fail on a 16-bit platform, I’m not ever allowed to compile — even on a 64-bit platform where such a cast would always succeed.<p>But therein lies the original questionable turn that Rust made wrt usize--there&#x27;s no accounting for <i>future</i> platforms.<p>What makes C so portable is precisely the notion of C&#x27;s integer ranking system and implicit conversions. By guaranteeing relative rank and permitting implicit widening conversions, most issues on most <i>future</i> architectures have been accommodated. It&#x27;s not perfect, but the the value for your money is immense. The vast majority of issues like this go away.<p>And what do you lose by permitting implicit widening conversions? There are some potential correctness issues. For example, subexpressions computing bitmasks might not behave as expected when converted to a wider type in an outer expression. But this same problem exists in your proposed solutions and explicit conversions, generally. I would even consider implicit conversions safer because we can always add additional rules (optional or mandatory) that capture these cases (e.g. -Wwidth-dependent-shift-followed-by-implicit-widening), whereas explicit conversions usually have the effect of short-circuiting stronger type checking. (Unless you go the C++ route and add a panoply of conversion operators. But better hope you chose the right one!)<p>You lose the ability for a build to fail on target X because the conversion wouldn&#x27;t work on target Y. But that requirement is fundamentally in conflict with accommodating <i>future</i> architectures, and incentives the type of explicit conversions that could hide or complicate future porting issues.<p>Regarding this footnote:<p>&gt; [8] For reasons that are unclear to me, uintptr_t is an optional type in C99. However, I don’t know of any platforms which support C99 but don’t define it.<p>AFAIU, the reason is exactly because the committee foresaw that not all architectures could accommodate conversions between object pointers and integers. Relatedly, the C standard DOES NOT permit conversions between object pointers and function pointers, which also means the C standard DOES NOT permit conversions between function pointers and uintptr_t, even if uintptr_t is defined. Both capability systems and memory architectures already existed that couldn&#x27;t accommodate the latter conversions. The value of function pointer&#x2F;integer conversions was much less than object pointer&#x2F;integer conversions, so they defined uintptr_t only for object pointers, and made uintptr_t optional.<p>Function pointer&#x2F;object pointer conversions are widely supported by C compilers, but this is an extension that makes code non-conforming. See C11 J.5.7. Note that non-conforming does not mean undefined; it just means that such code is not C code as defined by the standard and beyond its purview. It creates a headache for POSIX, which defines a singular interface, dlsym, for acquiring <i>both</i> object and function references.
klodolph将近 3 年前
&gt; […] but what would a signed memory address mean and what happens if&#x2F;when we want to use all 64-bits to represent memory addresses […]<p>Half of this space is reserved for the kernel.
评论 #31424572 未加载
评论 #31424786 未加载
RcouF1uZ4gsC将近 3 年前
If you are interested in a C++ library that makes using integers a lot safer, take a look at Boost.SafeInt<p><a href="https:&#x2F;&#x2F;www.boost.org&#x2F;doc&#x2F;libs&#x2F;1_79_0&#x2F;libs&#x2F;safe_numerics&#x2F;doc&#x2F;html&#x2F;introduction.html" rel="nofollow">https:&#x2F;&#x2F;www.boost.org&#x2F;doc&#x2F;libs&#x2F;1_79_0&#x2F;libs&#x2F;safe_numerics&#x2F;doc...</a>
评论 #31425075 未加载
MauranKilom将近 3 年前
&gt; Undefined behaviour on integer types is a terrible idea (though unspecified behaviour might have a place).<p>Would unspecified behavior be sufficient to attain those compiler optimizations that are the reason for keeping signed integer overflow undefined in newer C and C++ versions?
评论 #31423304 未加载
phdelightful将近 3 年前
Is there a C++ library that implements static integer types with these ideas? In principle the operations don’t seem complicated, but there are probably enough edge cases that it’s tricky to get it all right.