将未签名的char *缓冲区分配给字符串 [英] Assigning unsigned char* buffer to a string

查看:48
本文介绍了将未签名的char *缓冲区分配给字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能之前曾被问过,但我找不到我真正需要的东西.

This question might be asked before but I couldn't find exactly what I need.

我的问题是,我有一个缓冲区,该缓冲区由从Web服务下载的数据加载.缓冲区采用无符号char *格式,其中末尾没有'\ 0'.然后我有一个poco xml解析器需要一个字符串.

My problem is, I have a buffer loaded by data downloaded from a webservice. The buffer is in unsigned char* form in which there is no '\0' at the end. Then I have a poco xml parser needs a string.

我尝试将其分配给字符串valgrind时发现丢失了一些数据.(请参见下文)

I tried assigning it to string valgrind found some lost data. (see below)

下面是代码:

DOMParser::DOMParser(unsigned char* consatData, int consatDataSize,
    unsigned char* lagData, int lagDataSize) {

Poco::XML::DOMParser parser;
std::string consat;
consat.assign((const char*) consatData, consatDataSize);
pDoc = parser.parseString(consat);
ParseConsat();
}

Poco xml解析器确实有一个ParseMemory,它需要一个const char *和数据大小,但是由于某种原因,它只会给我带来分段错误.

Poco xml parser does have a ParseMemory which need a const char* and size of data but for some reason it just gives me segmentation fault.

更新:这是valgrind结果的一部分:

Update: Here is a part of valgrind result:

==11880== 12,272 bytes in 1 blocks are possibly lost in loss record 1,126 of 1,143
==11880==    at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
==11880==    by 0x4491D05: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc+$
==11880==    by 0x4493F6F: std::string::_M_mutate(unsigned int, unsigned int, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==11880==    by 0x4494109: std::string::_M_replace_safe(unsigned int, unsigned int, char const*, unsigned int) (in /usr/lib/libstdc++.$
==11880==    by 0x44941AD: std::string::assign(char const*, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==11880==    by 0x804DE03: DOMParser::DOMParser(unsigned char*, int, unsigned char*, int) (DOMParser.cpp:27)

推荐答案

我的问题是如何获取不为null的数据终止于字符串

My question is how to get the data which is not null terminated to a string

使用适当的 std :: string 构造函数,如下所示:

Use the appropriate std::string constructor, like this:

std::string( (const char*) consatData, consatDataSize);

几乎等效地,使用 .assign()方法:

Nearly equivalently, use the .assign() method:

std::string consat;
consat.assign((const char*) consatData, consatDataSize);

我意识到这会引起泄漏等问题.

I realized it would cause problem such as leaking.

您认识不正确.字符串数据被复制到字符串中.没有泄漏.

You realize incorrectly. The string data is copied into the string. There is no leak.

注意:有人可能会说:"不要使用C样式的强制转换!".它们可能是正确的.您可能应该改用 reinterpret_cast< const char *>(consatData).我将C样式转换保留在原处,因为它们可以工作,并且因为您的原始代码使用了它.

Note: Someone may say, "Don't use C-style casts!" They are probably right. You probably should use reinterpret_cast<const char*>(consatData) instead. I left the C-style casts in place because they work, and because your original code uses it.

这篇关于将未签名的char *缓冲区分配给字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆