这是Visual C++ 2008的编译器错误吗

Is This a Compiler's Bug of Visual C++ 2008

本文关键字:编译器 错误 2008 Visual C++ 这是      更新时间:2023-10-16

我正在编写一个简单的模式客户端使用boost asio,但是,当Windows 7(64bit)中的Visual C 2008下的发行模式编译时,该程序将引起访问违规例外无辜的" std :: string s;"在功能处理程序中。欢迎任何建议。

#include <iostream>
#include <boost/asio.hpp>
#include <string>
#include <boost/algorithm/string.hpp>

typedef boost::asio::buffers_iterator<boost::asio::const_buffers_1> iterator_t;
typedef boost::iterator_range<iterator_t> range_t;
static const std::string LINE_END_MARK = "rn";
int main(int argc, char* argv[])
{
    boost::asio::streambuf _buf;
    std::ostream os(&_buf);
    os<<"ENDrn";
    iterator_t cursor = boost::asio::buffers_begin(_buf.data());
    iterator_t end = boost::asio::buffers_end(_buf.data());
    std::ostream_iterator<char> it(std::cout," ");
    std::copy(LINE_END_MARK.begin(), LINE_END_MARK.end(), it);
    range_t r(cursor, end);
    if(!boost::ends_with(r, LINE_END_MARK))
        return 0;
    return 1;
}

添加"无辜"自动变量时,您正在更改该函数的堆栈框架的布局。发生的事情是,堆栈上的某些变量(始终)损坏会移动。因此,当您以前浪费了一些没有注意到的内存位置时,您现在正在浪费更重要的东西(例如返回地址)。

正如评论中提到的马特球一样,代码中错误的可能性比编译器错误的概率大得多,尤其是在最近的编译器中。如果您仍然确信它是一个编译器错误,则可以通过与另一个编译器(例如GCC或Windows Port,mingw)来确认您的怀疑。