This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
about warning "cannot optimize loop, the loop counter may overflow"
- From: JÄdrzej Dudkiewicz <jedrzej dot dudkiewicz at gmail dot com>
- To: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Wed, 18 May 2011 14:18:33 +0200
- Subject: about warning "cannot optimize loop, the loop counter may overflow"
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.