你怎么做以避免在c ++中超出数组访问范围? [英] what you do to avoid out of bounds array access in c++?

查看:349
本文介绍了你怎么做以避免在c ++中超出数组访问范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好每一个,



我创建了一个数组但是当我得到一个出界的数组访问错误时,你可以帮我避免它。 br $> b $ b

等待回复。



谢谢。

Hello Every one,

I have created an array but when i am getting an out of bound array access error please can you help me to avoid it.

Much awaited for reply.

Thank you.

推荐答案

由于你没有显示任何代码,答案只能是一般的:保持在数组的范围内。除了在一些疯狂的越界位置访问之外,一个特殊情况更常见:如果你有一个大小为10的数组,那么10不是一个有效的索引。因为C ++中的数组是基于0的。所以在这种情况下,有效索引是0到9。
Since you didn't show any code the answer can only be a general one: Stay inside the bounds of the array. Apart from accessing at some wildly out of bounds position, one particular case is more common: If you have an array with a size of 10 then 10 isn't a valid index. Because arrays in C++ are 0-based. So in this case, valid indices are 0 to 9.


你不能超出范围, C / C ++ 开发人员已经确切地说。也就是说,您可以使用 std :: vector 而不是普通数组:它提供 std :: vector :: at - cppreference.com [ ^ ]如果您尝试进行越界访问,则抛出异常的方法。
You must not go out of bounds, the C/C++ developer has to be precise. That said, you could use std::vector instead of a plain array: it provides the std::vector::at - cppreference.com[^] method that throws an exception if you try to make an out-of-bounds access.


这篇关于你怎么做以避免在c ++中超出数组访问范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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