about warning "cannot optimize loop, the loop counter may overflow"

Jędrzej Dudkiewicz jedrzej.dudkiewicz@gmail.com
Wed May 18 14:36:00 GMT 2011


Hi.

I have the following code:

static const std::string paths[] = { "file1", "file2", "file2" };
for (size_t i = 0; i < (sizeof(paths) / sizeof(paths[0])); ++i)
{
    if (access(paths[i], F_OK | R_OK) == 0)
    {
        return std::string(paths[i]);
    }
}

It results in error:

error: cannot optimize loop, the loop counter may overflow
[-Werror=unsafe-loop-optimizations]

(well, warning turned to error with -Werror) in the line "for (size_t i = ...".

Changing "std::string" in array's declaration into "char* const"
removes the warning (error). Why is that? I thought that this warning
comes from optimizer, which knows the exact size of "std::string"
object?
-- 
Jędrzej Dudkiewicz

I really hate this damn machine, I wish that they would sell it.
It never does just what I want, but only what I tell it.



More information about the Gcc-help mailing list