Dyl*_*ens 6 c++ boost streambuf boost-asio
我使用asio :: streambuf遇到了问题,我希望有人可以告诉我,如果我错误地使用了这个类.当我运行这个示例代码时,它会出现段错误.为什么?
为了使事情更加混乱,此代码适用于Windows(Visual Studio 2008),但不适用于Linux(使用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;
}
Run Code Online (Sandbox Code Playgroud)
我使用和看到的 asio::streambuf 通常使用的方式是与 std::ostream 或 std::istream 一起使用,如下所示:
boost::asio::streambuf Stream;
std::ostream os(&Stream);
int SetValue = 0xaabbccdd;
os.write(reinterpret_cast<const char*>(&SetValue), sizeof(SetValue));
Run Code Online (Sandbox Code Playgroud)
我不确定为什么您的代码不起作用,但如果上面的代码确实有效,那么逐步执行它可能会显示出与您的代码之间的一些差异。另外,它崩溃在哪一行?
| 归档时间: |
|
| 查看次数: |
1785 次 |
| 最近记录: |