【问题标题】:What is the meaning of this splint warning and what might I be doing wrong?这个夹板警告的含义是什么?我可能做错了什么?
【发布时间】:2010-09-07 03:31:18
【问题描述】:

这是代码行:

bool cpfs_utimens(struct Cpfs *, char const *path, struct timespec const[2]);

运行夹板 3.1.2 会生成此警告:

cpfs.h:21:74: Function parameter times declared as manifest array (size
                 constant is meaningless)
  A formal parameter is declared as an array with size.  The size of the array
  is ignored in this context, since the array formal parameter is treated as a
  pointer. (Use -fixedformalarray to inhibit warning)

命名参数没有区别。

【问题讨论】:

    标签: c linux gcc splint


    【解决方案1】:

    这意味着当你声明参数struct timespec const[2]时,[]之间的2不是必需的。将您的代码更改为:

    bool cpfs_utimens(struct Cpfs *, char const *path, struct timespec const[]);
    

    在 C/C++ 中,您不能要求某个大小的数组作为参数,因为数组被视为指针,而指针没有大小。

    【讨论】:

    • 如果你想传值,你可以把它包装在struct中。
    • @Matt:参见例如C FAQ。你也不能按值返回数组。
    • @Matt:声明一个返回类型为数组类型的函数是非法的——即使不是,你也永远无法成功地将数组传递给return,因为它总是衰减为指向其第一个元素的指针。
    • @Jack Kelly re apostrophe nit...既然你问了,这种 nit 的 SO 方法是(借用著名维基的一句话)大胆地去修复它,如果你有足够的代表来编辑它。如果没有,耐心通常会产生一个有足够代表的用户来修复它。这样一来,大部分破损的窗户都会得到修复,而无需大惊小怪。
    • 您询问的C99参考是第6.7.5.3/1(禁止返回数组类型的类型)和6.7.5.3/7(数组类型的参数更改为指针类型)
    【解决方案2】:

    在 C99 中(因为您使用 bool),您可以通过像这样添加 static 来要求参数数组的最小长度

    bool cpfs_utimens(struct Cpfs *, char const *path, struct timespec const[static 2]);
    

    签名(如果 C 中有这样的东西)仍然是指针参数的签名,我想。

    (而且我还不知道任何现有的编译器可以从这些信息中做一些明智的事情。)

    【讨论】:

    • @Matt Joiner:至少gcc 实现了句法部分;-) 我想,真正的测试很难实现。您要么必须处理指针上的某种不变量 (is larger than),要么严格限制为正确大小的数组对象。
    猜你喜欢
    • 2022-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多