首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于数组下标检查验证有关问题,望大家进来看下

2012-05-27 
关于数组下标检查验证问题,望大家进来看下大家好,关于c语言数组下标检查问题,主要是为了提高效率,那么能不

关于数组下标检查验证问题,望大家进来看下
大家好,关于c语言数组下标检查问题,主要是为了提高效率,那么能不能写两个程序,一个不用对下标进行检查,另外一个对数组下标进行检查,然后通过运行时间来验证效率问题?不知道如何让编译器对数组下标进行检查,望大家帮我解答,感激不尽。。。。

[解决办法]
写都不要写就知道检查下标要消耗掉效率。肯定要执行额外的指令才能检查的嘛。那么执行了指令就要消耗程序的时间的呢
[解决办法]
自己实现对数组下标的检查,编译器好像不具备这样的功能。
[解决办法]
对数组下标进行检查是个运行时期的事情,编译器是没有能力在编译阶段检查出下标使用不对。

现代的编译器可以给个选项,使得编译器多生成一些代码,这些代码可以在运行时检查是否有越界发生,这样一来,自然效率要稍微差些。

很简单的想法,检查要比不检查多做些事情,所以会效率会差些。

[解决办法]
如果 lz 的意思是越界检查

效率只是原因之一,主要是 c 的思想 -- 你要为自己的行为负责

和很多时候无法实现,比如 数组作为输入参数,malloc 分配的数组空间
[解决办法]
一种方法:
在最后一个字节的后面对应内存地址处设置内存值改变断点:
Setting a Breakpoint When the Value at a Specified Memory Address Changes
To break when the value at a specified memory address changes

From the Edit menu, click Breakpoints.
Click the Data tab of the Breakpoints dialog box.
In the Expression text box, type the memory address for the byte.
For a word or doubleword memory address, enclose the address in parentheses, and precede it with a cast operator. For example, WO(21406036) for the word at memory location 21406036. Use the cast operator BY for a byte (optional), WO for a word, or DW for a doubleword. (The debugger interprets all integer constants as decimal unless they begin with zero (0) for octal or zero and x (0x) for hexadecimal.)
In the Number Of Elements text box, type the number of bytes, words, or doublewords to monitor. If you used the BY operator in the Expression field, specify the number of bytes. If you used WO, specify the number of words. If you used DW, specify the number of doublewords.
Click OK to set the breakpoint.

热点排行