C++ std::vector已满,但不显示数据

发布于 2024-11-03 13:40:34 字数 2428 浏览 4 评论 0原文

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉鱼一梦 2024-11-10 13:40:34

假设调试器显示的数据是正确的,消息缓冲区中似乎有一些二进制零。

请记住,二进制 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 any 0 characters (and perhaps other non-printable stuff as well - depends a bit on what your exact intentions are).

瞄了个咪的 2024-11-10 13:40:34

扩展亚历山大的答案(这是正确的),如果你真的想将二进制数据写入标准输出,那么你可以这样做:

std::cout.write(&m_buff[0], m_buff.size());

Extending Alexander's answer (which is correct), if you genuinely want to write binary data to standard output, here's how you do it:

std::cout.write(&m_buff[0], m_buff.size());
揪着可爱 2024-11-10 13:40:34

当您使用 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 how operator<<(std::ostream&, const char*) has to interpret it's argument, as the c_str() conversion prevents use of operator<<(std::ostream&, const std::string&) which would have access to the properly size()-aware std::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 use std::cout.write(address, number_of_bytes) for binary output providing the address as per &message[0], message.data() or even message.c_str() though you don't need additional NUL-termination and a poor implementation may waste time and memory creating one, along with size().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文