编程与软件工程hamberg.no
鲜为人知的 C 特性:参数声明中的静态数组索引
A little-known C feature: Static array indices in parameter declarations (2013)
2013 — 2018,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇 2013 年的博客介绍了 C99 中一个鲜为人知的特性:在函数参数声明中写 int a[static 10],表示传入的指针必须指向至少 10 个元素且不能为 NULL。编译器可据此做检查和优化,但作者和评论都指出实际支持很有限。
为什么经久不衰:它揭示了一个连资深 C 程序员都常不知道的语言特性,也折射出 C 标准中 static 关键字被反复复用的设计问题,时隔多年仍具讨论价值。
HN 评论精选
调侃 static 关键字在 C 中被过度复用
我写了 25 年 C 都不知道这个特性,而它又是 static 关键字被重载的又一例,颇有诗意。仿佛标准委员会到处找语法上能塞进 static 的地方,再反过来给它编一个含义。
tptacek查看英文原文
There is something personally poetic (is that a thing? it's poetic to me, but maybe not to you?) about a C feature I was unaware of despite working in the language for 25 years being yet another way in which the keyword "static" is overloaded. It's as if they go around looking for places in the grammar they can unambiguously stick "static" and then back-rationalize a meaning for it.
指出该特性存在规范漏洞,可能被后续标准移除
这个特性可能会在下一版 C 标准中被移除(或已经移除),因为带 static 大小的声明与不带的声明是“兼容”的,会导致不健全:比如 void foo(int a[static 3]) 和 void foo(int a[]) 可以同时声明同一函数,规范没说定义时是否要遵守 static 注解。
lenary查看英文原文
They are likely to remove this in the next C standard (or may have already), because a declaration with a static size is “compatible” with a declaration with no static size, and thus you can get unsoundness. So your compiler should accept (though the semantics of static suggest this program would be wrong): void foo(int a[static 3]); void foo(int a[]); According to the spec, this is fine, but when you come to define the function foo, should it respect the ‘static’ annotation? The spec doesn’t say, and doing static analysis of subsumption for the expressions after ‘static’ is much more complex to keep sound than C prefers in its specification.
实测 gcc 不检查该注解,clang 可以
我试了各种警告选项,gcc 始终不报错;clang 则能对传 NULL 给 [static 1] 参数的代码给出警告。gcc 连直接传空指针都静默通过,令人失望。
adrusi查看英文原文
Try as I might I can't get gcc to produce warnings for this (invocation: gcc --std=c99 -Wall -Werror -Wpedantic -Wnonnull). I can get clang to do so however. I didn't expect to get any compiler warnings for code that passes NULL to a [static 1] parameter, so I wasn't surprised when this code compiled silently: #include <stdio.h> void end(int foo[static 1]) { printf("%d\n", *foo); } void middle(int *foo) { end(foo); } int main() { middle(NULL); return 0; } But I was a bit disappointed that the following compiled without warning as well: #include <stdio.h> void end(int foo[static 1]) { printf("%d\n", *foo); } int main() { int *foo = NULL; end(foo); return 0; }
对比 C++ 中用引用和模板达到类似目的
在 C++ 里可以按引用传数组实现类似效果:void foo(int (&foo)[10]),但语义是“恰好 10 个”而非“至少 10 个”;还可以用模板获取编译期大小,例如实现一个安全的 safe_strcpy。
matthavener查看英文原文
In C++, you can do this by passing the array by reference: void foo(int (&foo)[10]) {} However, that is not "minimum of 10" but "exactly 10". You can also get the size at compile time: template <size_t N> void foo(int (&foo)[N]) { int newarry[N+2]; } I've used the above for something like this: template <size_t N> void safe_strcpy(char (&buf)[N], const char *src) { strncpy(buf, src, N-1); buf[N-1] = 0; }
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选