访问数组超越极限 [英] Accessing array beyond the limit

查看:115
本文介绍了访问数组超越极限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大小100两个字符数组(字符数组1 [100],CHAR数组2 [100])。现在我只是想检查是否有人正在访问阵列超越极限与否。对于数组1和数组2的必要,因为假设分配的内存是连续的手段作为ARRAY1完成,然后开始数组2。现在,如果有人写:数组1 [101],其概念错误的,但编译器会发出警告,但不会崩溃。那么,如何可以检测到这种问题并解决它?

I have two character array of size 100 (char array1[100], char array2[100]). Now i just want to check whether anybody is accessing array beyond the limit or not. Its necessary because suppose allocated memory for array1 and array2 are consecutive means as the array1 finish then array2 starts. Now if anyone write: array1[101], conceptually its wrong but compiler will give warning but will not crash. So How can i detect this problems and solve it?

更新1:

我已经有行15000一code。并为code我要检查这个条件,我可以调用我的功能,但不能更改的书面code。请你照这个建议我。

I already have a code of line 15,000. And for that code i have to check this condition and i can invoke my functions but cannot change the written code. Please suggest me according to this.

推荐答案

我对这个最初的反应是包装在一个函数或方法访问这些数组,并发送该指数作为参数。如果索引出界,抛出一个异常,或以其他方式报告错误。

My initial response to this would be to wrap the access to these arrays in a function or method and send the index as a parameter. If the index is out of bounds, raise an exception or report the error in some other way.

编辑:
当然,这是一个运行时prevention。不知道你怎么会在编译时检查这一点,如果编译器无法checkt这个给你。此外,由于Kolky已经指出的那样,它会更容易回答这个问题,如果我们知道您正在使用哪种语言。

This is of course a run-time prevention. Don't know how you would check this at compile time if the compiler cannot checkt this for you. Also, as Kolky has already pointed out, it'd be easier to answer this if we know which language you are using.

这篇关于访问数组超越极限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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