strlen()在for循环中调用多少次?

时间:2020-12-01 23:54:37

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?

下面的strlen()函数是否只被调用一次(存储的值用于进一步比较);或者每次进行比较时都会调用它?

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

6 个解决方案

#1


27  

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.

这是依赖于实现的。通常,它每次都被调用,但是,如果编译器可以看到该单词永远不会改变,并且strlen是一个纯函数(没有副作用),它可以解除调用。

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

请参阅:http://underhanded.xcott.com/?page_id = 15,了解有关此漏洞的一个众所周知的示例。 :-)

#2


8  

It will be evaluated for every iteration of the loop (edit: if necessary).

它将针对循环的每次迭代进行评估(编辑:如果需要)。

Like Tatu said, if word isn't going to change in length, you could do the strlen call before the for loop. But as Chris said, the compiler may be good enough to realize that word can't change, and eliminate the duplicate calls itself.

就像Tatu说的那样,如果单词的长度不会改变,你可以在for循环之前进行strlen调用。但正如克里斯所说,编译器可能已经足够好,可以意识到这个词不会改变,并且可以消除重复的调用本身。

But if word can change in length during the loop, then of course you'll need to keep the strlen call in the loop condition.

但是如果在循环期间word可以改变长度,那么当然你需要将strlen调用保持在循环条件中。

#3


6  

I'll sometimes code that as ...

我有时会将其编码为......

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

... so that strlen is only called once (to improve performance).

...所以strlen只被调用一次(以提高性能)。

#4


1  

The number of times strlen(word) is executed depends on:

strlen(word)执行的次数取决于:

  1. If word is declared as constant (the data is constant)
  2. 如果word被声明为常量(数据是常量)
  3. Or the compiler can detect that word is not changed.
  4. 或者编译器可以检测到该单词未被更改。

Take the following example:

采用以下示例:

char word[256] = "Grow";

for (i = 0; i < strlen(word); ++i)
{
  strcat(word, "*");
}

In this example, the variable word is modified withing the loop:
0) "Grow" -- length == 4
1) "Grow*" -- length == 5
2) "Grow**" -- length == 6

在这个例子中,变量字用循环修改:0)“Grow” - length == 4 1)“Grow *” - length == 5 2)“Grow **” - length == 6

However, the compiler can factor out the strlen call, so it is called once, if the variable word is declared as constant:

但是,编译器可以分解strlen调用,因此如果变量word被声明为常量,则调用一次:

void my_function(const char * word)
{
  for (i = 0; i < strlen(word); ++i)
  {
     printf("%d) %s\n", i, word);
  }
  return;
}

The function has declared that the variable word is constant data (actually, a pointer to constant data). Thus the length won't change, so the compiler can only call strlen once.

该函数声明变量字是常量数据(实际上是指向常量数据的指针)。因此长度不会改变,因此编译器只能调用一次strlen。

When in doubt, you can always perform the optimization yourself, which may present more readable code in this case.

如果有疑问,您可以自己执行优化,在这种情况下可能会提供更易读的代码。

#5


0  

strlen checks the lenght of the provided string. Which means that if the lenght is 10. Your iteration will keep on going as long as i is below 10.

strlen检查提供的字符串的长度。这意味着如果长度是10.只要我低于10,你的迭代就会继续。

And in that case. 10 times.

在那种情况下。 10倍。

Read more about loops

阅读更多关于循环

#6


0  

It will be called for each iteration. The following code only calls strlen function once.

每次迭代都会调用它。以下代码仅调用strlen函数一次。

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

#1


27  

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.

这是依赖于实现的。通常,它每次都被调用,但是,如果编译器可以看到该单词永远不会改变,并且strlen是一个纯函数(没有副作用),它可以解除调用。

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

请参阅:http://underhanded.xcott.com/?page_id = 15,了解有关此漏洞的一个众所周知的示例。 :-)

#2


8  

It will be evaluated for every iteration of the loop (edit: if necessary).

它将针对循环的每次迭代进行评估(编辑:如果需要)。

Like Tatu said, if word isn't going to change in length, you could do the strlen call before the for loop. But as Chris said, the compiler may be good enough to realize that word can't change, and eliminate the duplicate calls itself.

就像Tatu说的那样,如果单词的长度不会改变,你可以在for循环之前进行strlen调用。但正如克里斯所说,编译器可能已经足够好,可以意识到这个词不会改变,并且可以消除重复的调用本身。

But if word can change in length during the loop, then of course you'll need to keep the strlen call in the loop condition.

但是如果在循环期间word可以改变长度,那么当然你需要将strlen调用保持在循环条件中。

#3


6  

I'll sometimes code that as ...

我有时会将其编码为......

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

... so that strlen is only called once (to improve performance).

...所以strlen只被调用一次(以提高性能)。

#4


1  

The number of times strlen(word) is executed depends on:

strlen(word)执行的次数取决于:

  1. If word is declared as constant (the data is constant)
  2. 如果word被声明为常量(数据是常量)
  3. Or the compiler can detect that word is not changed.
  4. 或者编译器可以检测到该单词未被更改。

Take the following example:

采用以下示例:

char word[256] = "Grow";

for (i = 0; i < strlen(word); ++i)
{
  strcat(word, "*");
}

In this example, the variable word is modified withing the loop:
0) "Grow" -- length == 4
1) "Grow*" -- length == 5
2) "Grow**" -- length == 6

在这个例子中,变量字用循环修改:0)“Grow” - length == 4 1)“Grow *” - length == 5 2)“Grow **” - length == 6

However, the compiler can factor out the strlen call, so it is called once, if the variable word is declared as constant:

但是,编译器可以分解strlen调用,因此如果变量word被声明为常量,则调用一次:

void my_function(const char * word)
{
  for (i = 0; i < strlen(word); ++i)
  {
     printf("%d) %s\n", i, word);
  }
  return;
}

The function has declared that the variable word is constant data (actually, a pointer to constant data). Thus the length won't change, so the compiler can only call strlen once.

该函数声明变量字是常量数据(实际上是指向常量数据的指针)。因此长度不会改变,因此编译器只能调用一次strlen。

When in doubt, you can always perform the optimization yourself, which may present more readable code in this case.

如果有疑问,您可以自己执行优化,在这种情况下可能会提供更易读的代码。

#5


0  

strlen checks the lenght of the provided string. Which means that if the lenght is 10. Your iteration will keep on going as long as i is below 10.

strlen检查提供的字符串的长度。这意味着如果长度是10.只要我低于10,你的迭代就会继续。

And in that case. 10 times.

在那种情况下。 10倍。

Read more about loops

阅读更多关于循环

#6


0  

It will be called for each iteration. The following code only calls strlen function once.

每次迭代都会调用它。以下代码仅调用strlen函数一次。

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