I\'m not sure if the following code can cause redundant calculations, or is it compiler-specific?
for (int i = 0; i < strlen(ss); ++i)
{
// blabla
}
<
As of today (January 2018), and gcc 7.3 and clang 5.0, if you compile:
#include
void bar(char c);
void foo(const char* __restrict__ ss)
{
for (int i = 0; i < strlen(ss); ++i)
{
bar(*ss);
}
}
So, we have:
ss
is a constant pointer.ss
is marked __restrict__
ss
(well, unless it violates the __restrict__
).and still, both compilers execute strlen()
every single iteration of that loop. Amazing.
This also means the allusions/wishful thinking of @Praetorian and @JaredPar doesn't pan out.