C++ std::vector已满,但不显示数据
这是我的代码:
std::string message(m_buff.begin(), m_buff.end());
std::cout << "Client wrote sth!" << std::endl;
std::cout << message.c_str() << std::endl;
如果我在最后一行设置断点,启动调试器并查找 m_buff var,有一些数据,但没有显示。
这是数据(来自调试器):
m_buff [0x00000400](0x00,0x08 '',0x22 '"',0x00,0x57 'W',0x6f 'o',0x57 'W',0x00,0x03 '',0x03 '',0x05 '',0x34 '4',0x30 '0',0x36 '6',0x38 '8',0x78 'x',0x00,0x6e 'n',0x69 'i',0x57 'W',0x00,0x45 'E',0x44 'D',0x65 'e',0x64 'd',0x3c '<',0x00,0x00,0x00,0x05 '',0x6c 'l',0x19 '',0x1c '',0x04 '',0x46 'F',0x47 'G',0x47 'G',0x47 'G',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,...,...) std::vector<unsigned char,std::allocator<unsigned char> >
Heres my code:
std::string message(m_buff.begin(), m_buff.end());
std::cout << "Client wrote sth!" << std::endl;
std::cout << message.c_str() << std::endl;
If i set a breakpoint in the last line, start the debuger and lookup the m_buff var, there is some data, but it didnt get displayed.
Heres the Data (from debugger):
m_buff [0x00000400](0x00,0x08 '',0x22 '"',0x00,0x57 'W',0x6f 'o',0x57 'W',0x00,0x03 '',0x03 '',0x05 '',0x34 '4',0x30 '0',0x36 '6',0x38 '8',0x78 'x',0x00,0x6e 'n',0x69 'i',0x57 'W',0x00,0x45 'E',0x44 'D',0x65 'e',0x64 'd',0x3c '<',0x00,0x00,0x00,0x05 '',0x6c 'l',0x19 '',0x1c '',0x04 '',0x46 'F',0x47 'G',0x47 'G',0x47 'G',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,...,...) std::vector<unsigned char,std::allocator<unsigned char> >
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设调试器显示的数据是正确的,消息缓冲区中似乎有一些二进制零。
请记住,二进制
0
('\0'
) 用于指示字符串的结尾。因此,由于 m_buff 的第一个元素为零,因此该字符串的有效长度为 0。您也许应该迭代
m_buff
并将所有字节打印为十六进制序列 - 或过滤掉任何0
字符(也许还有其他不可打印的东西 - 取决于您的确切意图)。Assuming that the data displayed by the debugger is correct, it seems as if you're having some binary zeroes in your message buffer.
Remember, a binary
0
('\0'
) is used to indicate the end of a string. So, as the first element of m_buff is a zero, the string has effectively length 0.You should perhaps iterate
m_buff
and print all bytes as hex sequences - or filter out any0
characters (and perhaps other non-printable stuff as well - depends a bit on what your exact intentions are).扩展亚历山大的答案(这是正确的),如果你真的想将二进制数据写入标准输出,那么你可以这样做:
Extending Alexander's answer (which is correct), if you genuinely want to write binary data to standard output, here's how you do it:
当您使用
c_str()
时,它会返回字符串的文本内容,后跟 NUL 终止符。字符串的“文本”内容也以 0x00(NUL 终止符)开头。因此,作为 C 风格的 ASCIIZ 文本,您的字符串是空的(这就是operator<<(std::ostream&, const char*)
必须解释其参数的方式,如 c_str()转换阻止使用可以访问正确的size()
感知的operator<<(std::ostream&, const std::string&)
>std::string。因此 - 前面带有“Z”的 ASCIIZ:不会显示任何内容。如果您想以某种可见的屏幕形式显示 NUL,则必须选择某种表示形式。例如“\000”、“”等等,并一次移动一个字符来决定要打印的内容。
如果您只想以二进制形式写出数据,请不要使用
c.str( )
:您还可以使用std::cout.write(address, number_of_bytes)
进行二进制输出,根据&message[0]
提供地址,message.data()
甚至message.c_str()
尽管您不需要额外的 NUL 终止,但糟糕的实现可能会浪费时间和内存来创建一个终止,以及大小()
。When you use
c_str()
it returns the string's textual content followed by a NUL-terminator. Your string's "textual" content also starts with a 0x00 - a NUL terminator. Therefore, seen as C-style ASCIIZ text your string is empty (which is howoperator<<(std::ostream&, const char*)
has to interpret it's argument, as the c_str() conversion prevents use ofoperator<<(std::ostream&, const std::string&)
which would have access to the properlysize()
-awarestd::string
. So - ASCIIZ with a "Z" at the front: nothing will be displayed.If you want to display NULs in some visible on-screen form, you'll have to choose some representation such as "\000", "" whatever, and move through your string one character at a time deciding what to print.
If you just want to write the data out in a binary form, don't use
c.str()
: you can also usestd::cout.write(address, number_of_bytes)
for binary output providing the address as per&message[0]
,message.data()
or evenmessage.c_str()
though you don't need additional NUL-termination and a poor implementation may waste time and memory creating one, along withsize()
.