2009-07-22 28 views
7

我最近升级到GCC 4.4(MinGW的TDM版本)和现在后续的代码产生这些警告:C++ GCC4.4警告:数组下标是以上数组界限

在成员函数“无效控制台::打印(常量的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()的参数,通过将它包装在一个字符串()构造函数中,警告消失,谢谢大家的帮助:)

boost::split(tokens, newLine, boost::is_any_of(string("\n"))); 
+2

推测编译器也给出了错误的行号?请通过评论在您的代码中注明。 – 2009-07-22 22:03:59

回答

3

得到了同样的错误。作为一种变通方法我更换

is_any_of(" ") 

is_from_range(' ', ' ') 

这也可能会略微高效。

1

我注意到你的循环在这里改变了字符串的长度,但没有更新循环终止条件。这可能是你问题的根源吗?

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++; 
    } 
3

可能有些事情要与一个或多个这些GCC的bug:

GCC bugzilla search results for "Warning: array subscript is above array bounds"

不是所有的人都是有效的,但也有,如果你搜索周围,也有些固定的:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37861

所以我敢肯定有东西对那里发生的。基于评论,我会尝试编译没有优化,看看它是否消失。

我使用的标准算法(的std ::去掉,我认为),通过迭代器参数,一个虚假的界限警告:

myarray, 
myarray + sizeof(myarray)/sizeof(*myarray) 

,我敢肯定是界限。不过,只是在玩具代码中,所以我只是绕过它。如果海湾合作委员会真的在投掷狡猾的警告,那么你只需要仔细检查你的代码,直到它被修复。