编译器考试解释

时间:2013-03-01 15:54:21

标签: compiler-construction invariants induction

我正在准备编译器考试,我在过去的论文中发现了以下两个问题:我不知道如何回答:

1. Array bounds checking requires code to check each array subscript to ensure it
falls within the array's declared range. How could induction variable analysis be
used to avoid repeated bounds checks in "for" loops.

2. Give a short example in Java in which one array address calculation is loop-
invariant and another array address is an induction variable.

我知道什么是不变量和归纳变量意味着但我真的不知道在解释这两个解决方案时对我的期望是什么。

如果有人帮我解释,我将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

第一个非常简单 - 由于每次迭代时感应变量上升固定量,您可以预先计算用作数组下标的任何感应变量的最大值,并检查它是否超过数组边界。在执行循环之前,您只需执行一次,而不是在每次迭代时检查下标。

至于第二种情况,有很多例子(在各种语言中,包括Java)的循环不变和归纳变量循环 - 只有谷歌这些条款。

相关问题