2011-02-11 3 views
5

asio :: streambuf를 사용하는 동안 문제가 발생했습니다. 클래스를 잘못 사용하고 있는지 누군가가 알 수 있기를 바랍니다. 이 예제 코드를 실행하면 segfaults가 발생합니다. 왜?boost :: asio :: streambuf를 사용하는 코드가 segfault를 발생시킵니다.

이 코드는 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; 
} 
+0

가능한 버그 ... – niXman

+0

이 단지'asio :: streambuf'입니까, 아니면'std :: streambuf'도 같은 동작을 보이나요? –

+0

코어도 버려졌습니다. 컴파일 된 코드를 얻기 위해'#include '하십시오. – vissi

답변

1

내가 사용하고 ASIO 볼 :: streambuf의 일반적으로 사용했던 방법은 표준 : : ostream에 또는 표준 : : IStream을, 뭔가 등으로이다 : 나는 확실하지 않다

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

왜 코드가 작동하지 않지만 위의 코드가 작동하면 코드를 실행하면 코드와 약간의 차이가있을 수 있습니다. 또한 어느 선이 충돌하고 있습니까?

+0

"다음 줄은 프로그램을 segfault합니다"라는 주석 뒤에 코드가 충돌합니다. –

+0

@DylanKlomparens : 좋습니다. 내가 그 질문에 대답했을 때 거기에 있었습니까? 오랜만이야. :-) 당신이 지나가고있는 사이즈가 존중되지 않는 버그 같은데. –

관련 문제