C++ GCC4.4 警告:数组下标高于数组边界
题
我最近升级到 GCC 4.4(MinGW TDM 版本),现在以下代码会产生以下警告:
在成员函数“void Console::print(const std::string&)”中:
警告:数组下标高于数组边界
这是代码:
void Console::print( const std::string& str ) {
std::string newLine( str );
if( newLine.size() > MAX_LINE_LENGTH ) {
sf::Uint32 stringSize = newLine.size();
for( sf::Uint32 insertPos = MAX_LINE_LENGTH;
insertPos < stringSize; insertPos += MAX_LINE_LENGTH ) {
newLine.insert( insertPos, "\n" );
}
}
StringList tokens;
boost::split( tokens, newLine, boost::is_any_of("\n") );
for( StringList::iterator it = tokens.begin();
it != tokens.end(); ++it ) {
addLine( *it );
}
}
有任何想法吗?
这是正在做的优化......
也似乎是这一行导致了它:
boost::split( tokens, newLine, boost::is_any_of("\n") );
啊,是的,我找到了它,它是 boost::is_any_of() 的参数,通过将其包装在 string() 构造函数中,警告消失了,谢谢大家的帮助:)
boost::split( tokens, newLine, boost::is_any_of( string( "\n" ) ) );
解决方案
得到了同样的错误。作为一种变通方法我取代
is_any_of(" ")
与
is_from_range(' ', ' ')
其也可以稍微更有效。
其他提示
可能与以下一个或多个 GCC 错误有关:
GCC bugzilla 搜索结果“警告:数组下标高于数组边界”
并非所有这些都是有效的,但是如果您四处搜索,也有一些固定的:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37861
所以我很确定那里发生了一些事情。根据评论,我会尝试在不进行优化的情况下进行编译,看看它是否会消失。
我使用标准算法之一(我认为是 std::remove)并传递迭代器参数收到了虚假边界警告:
myarray,
myarray + sizeof(myarray)/sizeof(*myarray)
我很确定这是在范围内的。不过,它只是在玩具代码中,所以我只是回避它。如果 GCC 确实发出了狡猾的警告,您只需格外仔细地检查您的代码,直到它被修复。
我注意到这里你循环改变字符串的长度,但不更新循环的终止条件。难道这是你的问题的根源?
sf::Uint32 stringSize = newLine.size();
for( sf::Uint32 insertPos = MAX_LINE_LENGTH;
insertPos < stringSize; insertPos += MAX_LINE_LENGTH )
{
newLine.insert( insertPos, "\n" );
// You were probably wanting to put this here..
insertPos++;
stringSize++;
}
不隶属于 StackOverflow