-6

我正在使用 boost asio 编写一个简单的 memcached 客户端,但是,当在 Windows 7(64 位)的 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 = "\r\n";

int main(int argc, char* argv[])
{
    boost::asio::streambuf _buf;
    std::ostream os(&_buf);
    os<<"END\r\n";

    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;
}
4

2 回答 2

2

当您添加该“无辜”自动变量时,您正在更改该函数的堆栈框架的布局。发生的情况是,您(总是)破坏的堆栈上的某些变量被移动了。因此,当您以前丢弃一些未被注意到的内存位置时,您现在正在丢弃一些更重要的东西(例如返回地址)。

于 2012-10-31T03:37:41.723 回答
0

正如 Matt Ball 在评论中提到的,您的代码中出现错误的可能性远大于编译器错误的可能性,尤其是对于最近的编译器。如果您仍然确信这是一个编译器错误,您可以通过使用另一个编译器(例如 gcc 或其 Windows 端口 MinGW)进行编译来确认您的怀疑。

于 2012-10-31T03:35:29.527 回答