Code using boost :: asio :: streambuf calls segfault

I'm having problems using asio :: streambuf and I hope someone will tell me if I am using the class incorrectly. When I run this sample code, it is 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; } 
+6
c ++ boost boost-asio streambuf
source share
1 answer

The way I used and saw asio :: streambuf is usually used 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 does not work, but if it works above, then some difference may appear through it compared to your code. Also, on which line does it fall?

+1
source share

All Articles