我正在学习boost :: asio,遇到一个问题。基本上,asio :: buffer_cast()在我的机器上工作很奇怪。任何帮助将不胜感激。
我有以下示例代码,
#include <iostream>
#include <vector>
#include <sstream>
#include <iomanip>
#include <boost/asio.hpp>
int main ()
{
std::ostringstream type_stream;
type_stream << std::setw(4) << 100;
std::cout<<"type_stream:"<<type_stream.str()<<std::endl;
std::ostringstream head_stream;
head_stream << std::setw(10) << 92;
std::cout<<"head_stream:"<<head_stream.str()<<std::endl;
std::vector<boost::asio::const_buffer> buffers;
buffers.push_back(boost::asio::buffer(type_stream.str()));
buffers.push_back(boost::asio::buffer(head_stream.str()));
auto test = buffers[0];
const unsigned char* p1 = boost::asio::buffer_cast<const unsigned char*>(test);
std::cout<<"type_stream again:"<<std::string(reinterpret_cast<const char*>(p1))<<std::endl;
auto test2 = buffers[1];
const unsigned char* p2 = boost::asio::buffer_cast<const unsigned char*>(test2);
std::cout<<"head_stream again:"<<std::string(reinterpret_cast<const char*>(p2))<<std::endl;
return 0;
}
如果您在此处运行此代码:https://wandbox.org/permlink/4VkxJ4TFgjHzrath,它将正常工作。输出是
type_stream: 100
head_stream: 92
type_stream again: 100
head_stream again: 92
但是当将代码放入函数中并在我的机器上运行时,我得到的输出为
type_stream: 100
head_stream: 92
type_stream again: 92
head_stream again: 92
我在代码中做错什么了吗?在我的机器上,似乎第二个缓冲区覆盖了第一个缓冲区。我有gcc(Ubuntu 7.2.0-8ubuntu3.2)7.2.0,以及最新的boost :: asio。
答案 0 :(得分:1)
是的,有Undefined Behaviour。
问题是
buffers.push_back(boost::asio::buffer(type_stream.str()));
type_stream.str()
返回一个临时std::string
,因此在推送后缓冲区无效。
修复:
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>
std::string format_buf(int v, int width) {
std::ostringstream oss;
oss << std::setw(width) << v;
return oss.str();
}
int main() {
std::string type_str = format_buf(100, 4);
std::string head_str = format_buf(92, 10);
std::cout << "type_stream first: " << std::quoted(type_str) << std::endl;
std::cout << "head_stream first: " << std::quoted(head_str) << std::endl;
std::vector<boost::asio::const_buffer> buffers;
buffers.push_back(boost::asio::buffer(type_str));
buffers.push_back(boost::asio::buffer(head_str));
auto test = buffers[0];
const unsigned char *p1 = boost::asio::buffer_cast<const unsigned char *>(test);
std::cout << "type_stream again:" << std::quoted(reinterpret_cast<const char *>(p1)) << std::endl;
auto test2 = buffers[1];
const unsigned char *p2 = boost::asio::buffer_cast<const unsigned char *>(test2);
std::cout << "head_stream again:" << std::quoted(reinterpret_cast<const char *>(p2)) << std::endl;
}
打印
type_stream first: " 100"
head_stream first: " 92"
type_stream again:" 100"
head_stream again:" 92"
既然您实际上是在尝试进行固定宽度格式(?),为什么不使其变得更简单且更可靠:
char type[4] = {};
char head[10] = {};
bio::stream<bio::array_sink> tstream(type);
tstream << 100;
bio::stream<bio::array_sink> hstream(head);
hstream << 92;
它更健壮,因为使用您的代码,字段可能太宽(例如,包含12345的类型)
答案 1 :(得分:0)
在@sehe的帮助下,以下代码有效。但是我有新的担心。在char type[4] = {}; char head[10] = {}; char type2[2] = {};
函数内创建prepareBuffers()
可以吗?由于缓冲区是由buffers.push_back(boost::asio::buffer(type));
创建的,我怀疑当退出prepareBuffers()
函数时,类型将被破坏。但是main()
中的程序仍然可以访问缓冲区的内容。是否有任何副本发生?
#include <boost/asio.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>
namespace bio = boost::iostreams;
void prepareBuffers(std::vector<boost::asio::const_buffer> & buffers){
char type[4] = {};
char head[10] = {};
bio::stream<bio::array_sink> tstream(type);
bio::stream<bio::array_sink> hstream(head);
tstream << 555555;
hstream << 923;
std::cout << "type_stream first: " << std::quoted(type) << std::endl;
std::cout << "head_stream first: " << std::quoted(head) << std::endl;
buffers.push_back(boost::asio::buffer(type));
buffers.push_back(boost::asio::buffer(head));
auto test = buffers[0];
const unsigned char* p1 = boost::asio::buffer_cast<const unsigned char*>(test);
std::cout<<"in function type_stream again:"<<std::string(reinterpret_cast<const char*>(p1))<<std::endl;
test = buffers[1];
const unsigned char* p2 = boost::asio::buffer_cast<const unsigned char*>(test);
std::cout<<"in function head_stream again:"<<std::string(reinterpret_cast<const char*>(p2))<<std::endl;
}
int main() {
std::vector<boost::asio::const_buffer> buffers2;
prepareBuffers(buffers2);
auto test21 = buffers2[0];
const unsigned char* p1 = boost::asio::buffer_cast<const unsigned char*>(test21);
std::cout<<"type_stream again:"<<std::string(reinterpret_cast<const char*>(p1))<<std::endl;
auto test22 = buffers2[1];
const unsigned char* p2 = boost::asio::buffer_cast<const unsigned char*>(test22);
std::cout<<"type_stream again:"<<std::string(reinterpret_cast<const char*>(p2))<<std::endl;
return 0;
}