我想确认一下这种类型的代码
void f1 (int32_t p[10]);
void f2 (int32_t *p);
void b ()
{
int_32_t a[10];
f1(a);
f2(a);
}
当使用f2()时,由于可能会丢失数组边界的信息,因此在编码时不会发生这种情况。这是MISRA标准规则之一。
但AST-dump在两个函数声明之间没有区别:
|-FunctionDecl 0x2204140 <test.cpp:3:1, col:23> f1 'void (int32_t *)'
| -ParmVarDecl 0x2204040 <col:10, col:22> p 'int32_t *'
|-FunctionDecl 0x2204280 <line:4:1, col:20> f2 'void (int32_t *)'
| -ParmVarDecl 0x2204200 <col:10, col:19> p 'int32_t *'
我猜Clang确实会记录数组的大小(10),但是AST-dump似乎告诉我们相反的情况。有没有什么方法可以区分这两种情况呢?
f1
和f2
数组的大小均未知。这使得这个问题有些无意义。 - juanchopanzaint32_t(&p)[10]
将p
声明为大小为10的数组的引用。 - Jonathan Wakely