const正确性数组指针? [英] Const correctness for array pointers?

查看:139
本文介绍了const正确性数组指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人作了一个参数说,在现代的C,我们应该总是通过一个数组指针传递阵列功能,因为指针数组有强类型。例如:

Someone made an argument saying that in modern C, we should always pass arrays to functions through an array pointer, since array pointers have strong typing. Example:

void func (size_t n, int (*arr)[n]);
...

int array [3];
func(3, &array);

这听起来像它可能是一个好主意,以prevent各类型相关和数组超出边界错误。但它发生,我,我不知道该怎么const正确性应用于此。

This sounded like it could potentially be a good idea to prevent all kinds of type-related and array-out-of-bounds bugs. But then it occurred to me I don't know how to apply const correctness to this.

如果我无效FUNC(为size_t N,const int的(* ARR)[N])那么它是const正确性。但后来我再也无法通过阵列,因为不兼容的指针类型。 INT(*)[3] const int的(*)[3] 。限定符属于尖,在数据而不是指针本身。

If I do void func (size_t n, const int (*arr)[n]) then it is const correct. But then I can no longer pass the array, because of incompatible pointer types. int (*)[3] versus const int (*)[3]. The qualifier belongs to the pointed-at data and not to the pointer itself.

在来电显式转换会毁增加类型安全的整体思路。

An explicit cast in the caller would ruin the whole idea of increased type safety.

我如何申请const正确性作为参数传递数组指针?它是在所有可能的?

How do I apply const correctness to array pointers passed as parameters? Is it at all possible?

修改

正如信息,有人说,指针传递数组这样的想法可能是从MISRA C ++来源:2008年12年5月2日。例如,见<一href=\"http://www.codingstandard.com/rule/4-1-1-ensure-that-a-function-argument-does-not-undergo-an-array-to-pointer-conversion/\">PRQA's诚信度高C ++标准。

Just as info, someone said that the idea of passing arrays by pointer like this probably originates from MISRA C++:2008 5-2-12. See for example PRQA's high integrity C++ standard.

推荐答案

有没有办法做到这一点,除了中投。这是观念的显著缺点传递数组这种方式。

There is no way to do it except for the cast. This is significant drawback of the idea to pass arrays in this way.

<一个href=\"http://stackoverflow.com/questions/27897646/pointer-to-array-of-unspecified-size-p-illegal-in-c-but-legal-in-c\">Here是类似的线程所在的C规则相比,C ++的规则。我们可以从这种比较了C规则不那么精心设计的,因为你的使用情况是有效的,但C不会允许隐式转换的结论。另一个例子是 T转换** T const的* const的* ;这是安全的,但不能用C不允许的。

Here is a similar thread where the C rules are compared to the C++ rules. We could conclude from this comparison that the C rules are not so well designed, because your use case is valid but C doesn't allow the implicit conversion. Another such example is conversion of T ** to T const * const *; this is safe but is not allowed by C.

请注意,由于 N 不是一个恒定的前pression,那么 INT N,INT(* ARR)[N] 没有任何附加的类型安全相比, INT N,为int * ARR 。你还知道长度(N),它仍然是沉默的不确定的行为来访问出界了,无语不确定的行为传递一个数组,它是不实际长度 N

Note that since n is not a constant expression, then int n, int (*arr)[n] does not have any added type safety compared to int n, int *arr. You still know the length (n), and it is still silent undefined behaviour to access out of bounds, and silent undefined behaviour to pass an array that is not actually length n.

该技术在通过非VLA阵列,当如果你传递一个指针错误的长度的数组的编译器必须报告的情况下,更多的价​​值。

This technique has more value in the case of passing non-VLA arrays , when the compiler must report if you pass a pointer to an array of the wrong length.

这篇关于const正确性数组指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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