使用boost code :: ASIO ::流缓冲段错误导致 [英] Code using boost::asio::streambuf causes segfault

查看:242
本文介绍了使用boost code :: ASIO ::流缓冲段错误导致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用ASIO ::流缓冲遇到的问题,我希望,如果我使用的类错误地有人能告诉我。当我运行这个例子code它出现segfaults。为什么呢?

为了使事情变得更加混乱,这code适用于Windows(Visual Studio 2008中),但(用gcc 4.4.1)在Linux上不起作用。

 的#include<升压/ asio.hpp>
使用命名空间std;诠释的main()
{
        提高:: ASIO ::流缓冲流;        //把4个字节到流缓冲...
        INT的SetValue = 0xaabbccdd;
        Stream.sputn(reinter pret_cast<为const char *>(安培;的SetValue)的sizeof(的SetValue));        //消耗字节的3 ...
        Stream.consume(3);
        COUT<< Stream.size()&所述;&下; ENDL; //应该输出1        //获取最后一个字节...
        焦炭的GetValue;
        // ---------下一行出现segfaults程序----------
        Stream.sgetn(reinter pret_cast<字符*>(安培;的GetValue)的sizeof(的GetValue));
        COUT<< Stream.size()&所述;&下; ENDL; //应该输出0        返回0;
}


解决方案

我已经使用和ASIO看到::流缓冲通常采用的方法是使用的std :: ostream的或std :: istream的,是这样的:

 的boost ::支持ASIO ::流缓冲流;
的std :: ostream的OS(安培;流);
INT的SetValue = 0xaabbccdd;
os.write(reinter pret_cast<为const char *>(安培;的SetValue)的sizeof(的SetValue));

我不知道为什么你的code不工作,但如果上述不工作,然后通过它加强可能会出现一些差异对你的code。也是它崩溃哪一行呢?

I've experienced problems using asio::streambuf and am hoping someone can tell me if I'm using the class incorrectly. When I run this example code it segfaults. Why?

To make things more confusing, this code works on Windows (Visual Studio 2008), but does not work on Linux (with gcc 4.4.1).

#include <boost/asio.hpp>
using namespace std;

int main()
{
        boost::asio::streambuf Stream;

        // Put 4 bytes into the streambuf...
        int SetValue = 0xaabbccdd;
        Stream.sputn(reinterpret_cast<const char*>(&SetValue), sizeof(SetValue));

        // Consume 3 of the bytes...
        Stream.consume(3);
        cout << Stream.size() << endl; // should output 1

        // Get the last byte...
        char GetValue;
        // --------- The next line segfaults the program ----------
        Stream.sgetn(reinterpret_cast<char*>(&GetValue), sizeof(GetValue));
        cout << Stream.size() << endl; // should output 0

        return 0;
}

解决方案

The way I've used and seen asio::streambuf usually used is with std::ostream or std::istream, something like:

boost::asio::streambuf Stream;
std::ostream os(&Stream);
int SetValue = 0xaabbccdd;
os.write(reinterpret_cast<const char*>(&SetValue), sizeof(SetValue));

I'm not sure why your code doesn't work but if the above does work then stepping through it may show some difference vs. your code. Also which line is it crashing on?

这篇关于使用boost code :: ASIO ::流缓冲段错误导致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆