我最近升级到GCC 4.4 (MinGW TDM构建),下面的代码生成以下警告:
在成员函数'void::print(const::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" ) ) );发布于 2009-12-01 17:55:13
也有同样的错误。作为一种解决办法,我取代了
is_any_of(" ")使用
is_from_range(' ', ' ')这也可能更有效率。
发布于 2009-07-22 22:32:47
可能与GCC的一个或多个虫子有关:
GCC bugzilla搜索“警告:数组下标在数组边界上”的搜索结果
并不是所有的都是有效的,但是如果你四处搜索的话,也有一些固定的:
bug.cgi?id=37861
所以我很确定一定有什么事情发生了。基于这些评论,我会尝试编译而不进行优化,看看它是否会消失。
我使用标准算法之一(我认为是:remove)和传递迭代器参数得到了一个虚假的界警告:
myarray,
myarray + sizeof(myarray)/sizeof(*myarray)我很确定这是在界限之内。它只是在玩具代码中,所以我只是在它周围。如果GCC真的在发出不可靠的警告,那么您只需格外小心地检查代码,直到它被修复为止。
发布于 2009-07-22 22:18:53
我注意到您的循环正在改变字符串的长度,但没有更新循环终止条件。这会是你问题的根源吗?
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++;
}https://stackoverflow.com/questions/1168525
复制相似问题