C++ 是否有效,使用 std::string 来保存二进制数据,以避免手动动态内存管理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4655206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it valid, to use std::string to hold binary data, to avoid manual dynamic memory management
提问by Cheok Yan Cheng
Pay attention to base64_decode
in http://www.adp-gmbh.ch/cpp/common/base64.html
注意base64_decode
在http://www.adp-gmbh.ch/cpp/common/base64.html
std::string base64_decode(std::string const& encoded_string)
The function is suppose to return byte array
to indicate binary data. However, the function is returning std::string
. My guess is that, the author is trying to avoid from perform explicit dynamic memory allocation.
该函数假设返回byte array
以指示二进制数据。但是,该函数正在返回std::string
。我的猜测是,作者试图避免执行显式动态内存分配。
I try to verify the output is correct.
我尝试验证输出是否正确。
int main()
{
unsigned char data[3];
data[0] = 0; data[1] = 1; data[2] = 2;
std::string encoded_string = base64_encode(data, 3);
// AAEC
std::cout << encoded_string << std::endl;
std::string decoded_string = base64_decode(encoded_string);
for (int i = 0; i < decoded_string.length(); i++) {
// 0, 1, 2
std::cout << (int)decoded_string.data()[i] << ", ";
}
std::cout << std::endl;
getchar();
}
The decoded output is correct. Just want to confirm, is it valid to std::string
to hold binary data, to avoid manual dynamic memory management.
解码输出是正确的。只是想确认一下std::string
,持有二进制数据是否有效,以避免手动动态内存管理。
std::string s;
s += (char)0;
// s.length() will return 1.
回答by CB Bailey
Yes, you can store any sequence of char
in a std::string
. That includes any binary data.
是的,您可以将任何序列存储char
在std::string
. 这包括任何二进制数据。
回答by 6502
Yes. std::string can hold any char value ('\0'
has no special meaning). However I wouldn't be surprised finding some C++ functions (e.g. from external libraries) having problems with strings with embedded NULs.
是的。std::string 可以保存任何字符值('\0'
没有特殊含义)。但是,我不会惊讶地发现某些 C++ 函数(例如来自外部库)在嵌入 NUL 的字符串方面存在问题。
Anyway I don't understand what you are going to gain with an std::string
instead of std::vector<unsigned char>
that would make your intentions more clear and that offers more guarantees (e.g. that all the bytes are in contiguous not-shared memory so that you can pass &x[0]
to someone expecting a plain buffer for direct access).
无论如何,我不明白你会得到什么std::string
而不是std::vector<unsigned char>
它会让你的意图更清晰,并提供更多的保证(例如,所有字节都在连续的非共享内存中,这样你就可以传递&x[0]
给期待用于直接访问的普通缓冲区)。
回答by HernanBailo
I don't think it's completely valid.
Care must be taken with string and binary data because it uses internally char type and char depends in the implementation if it is defined as unsigned or signed type. I prefer to use basic_string<unsigned char>
and be sure of what i'm reading.
我不认为这是完全有效的。必须小心处理字符串和二进制数据,因为它在内部使用 char 类型,并且如果将 char 定义为无符号或有符号类型,则在实现中取决于它。我更喜欢使用basic_string<unsigned char>
并确定我正在阅读的内容。
回答by YeenFei
I dont think one should use std::string for byte-data-storage. The method provide aren't design to deal with byte-data and you will risk yourself since any changes (or "optimization") on std::string will break your code.
我不认为应该使用 std::string 进行字节数据存储。提供的方法不是为处理字节数据而设计的,您将面临风险,因为 std::string 上的任何更改(或“优化”)都会破坏您的代码。
回答by CrazyC
You need an array of character( not string) to store the binary data. Best is use vector.
您需要一个字符数组(不是字符串)来存储二进制数据。最好是使用矢量。