|
Boost Users : |
Subject: Re: [Boost-users] [asio] Run-time error with asio::streambuf
From: Igor R (boost.lists_at_[hidden])
Date: 2011-02-12 11:18:45
> 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).
>
> -- Dylan
>
> #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;
> }
If you write with:
std::ostream(&Stream).write(reinterpret_cast<const
char*>(&SetValue), sizeof(SetValue));
and read with:
std::istream(&Stream).read(reinterpret_cast<char*>(&SetValue),
sizeof(SetValue));
does it crash?
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net