多少次会的strlen()在这个被称为for循环? [英] how many times will strlen() be called in this for loop?

查看:92
本文介绍了多少次会的strlen()在这个被称为for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问strlen的()函数下面被调用只有一次(存储为进一步的比较值);或者它会被称为执行比较每一次?

 为(i = 0; I<的strlen(字);我++)
{ /* 做东西 */ }


解决方案

这是实现相关。通常情况下,它被称为每一次,但是,如果编译器可以看到永远不会改变,而的strlen 是纯函数(没有副作用),它可以解除呼叫。

请参阅: http://underhanded.xcott.com/?page_id=15 一个著名的例子这种被利用。 : - )

Will the strlen() function below get called just once (with the value stored for further comparisons); or is it going to be called every time the comparison is performed?

for (i = 0; i < strlen(word); i++)
{ /* do stuff */ }

解决方案

That's implementation-dependent. Usually, it gets called every time, but, if the compiler can see that word never changes, and that strlen is a pure function (no side effects), it can lift the call.

See: http://underhanded.xcott.com/?page_id=15 for a well-known example of this being exploited. :-)

这篇关于多少次会的strlen()在这个被称为for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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