What is the best alternative to calling strlen () in my loop state in C?

I read that bad practice calls strlen () in my loop cycle because it is an O (N) operation.

However, looking at alternatives, I see two possible solutions:

int len = strlen(somestring); for(int i = 0; i < len; i++) { } 

or...

 for(int i = 0; somestring[i] != '\0'; i++) { } 

Now the second option seems that it can have an advantage: 1) not to declare an unnecessary variable, and 2) if the length of the string should be changed in the loop, it should still reach the end, until the length is <i.

However, I am not sure. Which one is standard practice among C programmers?

+4
source share
6 answers

Usually the second is preferable.

Another popular form

 for (char* p = something; *p; p++) { // ... work with *p } 

One more

 char* p = something; char c; while ((c = *p++)) { // ... do something with c } 

(additional assignment () necessary so that some suspicious compilers do not give a warning that I can mean a comparison inside the while condition)

In fact, strlen pretty slow because it has to go through the whole line looking for the completion of 0. So, strlen is essentially implemented as

 int s = 0; while (*p++) s++; return s; 

(well, actually a slightly more optimized version of assembler is used).

Therefore, you should avoid using strlen if possible.

+9
source

This is preferable:

 for (int i = 0; str[i]; ++i) for (char* p = str; *p; ++p) 
+3
source

If some part of your loop can overwrite the NUL char at the end of your line, the version that calls strlen will still be completed until the end of your buffer. The second version can intercept the buffer and pass through all other memory. The strlen version is also easier to understand at a glance.

+3
source

Usually the second. If nothing else, the first should go through the line twice: once to find the length and work with each element again. On the other hand, if you have already written strlen code, it might be easier to just pull the strlen call out of the loop and get big benefits.

+1
source

The first approach looks like this to me:

 while(..) { Test end of string } /* strlen */ while(..) { Your Code } /* processing */ 

While the second approach looks like this:

 while(..) { Your Code + Test end of string } /* both */ 

IMHO, both approaches calculate approximately the same number of operations and I consider them equivalent. In addition, strlen is quite optimized , as already mentioned, and well tested. Moreover, the second approach looks like a premature optimization :) You would better check / analyze your code if necessary, and then optimize (in the end, this is just a linear algorithm).

However, you can consider the second approach, if processing may stop long before the end of the line (for example, find the first occurrence of the word).

0
source

As the other answers pointed out, while both options work, the second is more common. But comparing characters with "\ 0" is not what I would recommend. Type '\ 0' is an int, not a char. That is, it is of the same type as 0. However, it is more error prone to write "\ 0" than just 0, because a random inverse space can convert it to "0", which is not at all the same. This error can be easily detected by someone accidentally reading the code.

0
source

Source: https://habr.com/ru/post/1303025/


All Articles