C++:使用 std::string 在内存中移动 jpeg 是否安全?
我有一个 external_jpeg_func()
,它采用 char 数组中的 jpeg 数据来处理它。我无法修改这个功能。为了向其提供 char 数组,我做了如下操作:
//what the funcs take as inputs
std::string my_get_jpeg();
void external_jpeg_func(const char* buf, unsigned int size);
int main ()
{
std::string myString = my_get_jpeg();
external_jpeg_func(myString.data(), myString.length() );
}
我的问题是:使用字符串来传输 char 数组是否安全? jpeg(或任何二进制文件格式)是否有遇到“\0”等字符并导致数据丢失的风险?
I have an external_jpeg_func()
that takes jpeg data in a char array to do stuff with it. I am unable to modify this function. In order to provide it the char array, I do something like the following:
//what the funcs take as inputs
std::string my_get_jpeg();
void external_jpeg_func(const char* buf, unsigned int size);
int main ()
{
std::string myString = my_get_jpeg();
external_jpeg_func(myString.data(), myString.length() );
}
My question is: Is it safe to use a string to transport the char array around? Does jpeg (or perhaps any binary file format) be at risk of running into characters like '\0' and cause data loss?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,我的建议是使用
std::vector
,而不是std::string
;std::string
的危险在于它提供了c_str()
函数,并且大多数开发人员假设std::string
的内容是以 NUL 结尾的,尽管std::string
提供了size()
函数,该函数可以返回与在 NUL 处停止时得到的值不同的值。也就是说,只要您始终小心地使用带有大小参数的构造函数,并且小心地不要将.c_str()
传递给任何内容,那么使用字符串在这里。虽然使用
std::vector
相对于std::string
没有技术优势,但我认为它在与其他开发人员沟通方面做得更好该内容将被解释为任意字节序列而不是 NUL 终止的文本内容。因此,我会选择前者以增加可读性。也就是说,我使用过大量使用std::string
来存储任意字节的代码。事实上,C++ proto 编译器 生成这样的代码(不过,我应该补充一点,我不出于我提到的可读性原因,我认为这不是一个好的选择)。My recommendation would be to use
std::vector<char>
, instead ofstd::string
, in this case; the danger withstd::string
is that it provides ac_str()
function and most developers assume that the contents of astd::string
are NUL-terminated, even thoughstd::string
provides asize()
function that can return a different value than what you would get by stopping at NUL. That said, as long as you are careful to always use the constructor that takes a size parameter, and you are careful not to pass the.c_str()
to anything, then there is no problem with using a string here.While there is no technical advantage to using a
std::vector<char>
over astd::string
, I feel that it does a better job of communicating to other developers that the content is to be interpreted as an arbitrary byte sequence rather than NUL-terminated textual content. Therefore, I would choose the former for this added readability. That said, I have worked with plenty of code that usesstd::string
for storing arbitrary bytes. In fact, the C++ proto compiler generates such code (though, I should add, that I don't think this was a good choice for the readability reasons that I mentioned).std::string
不会特殊处理空字符,除非您没有给它明确的字符串长度。所以你的代码会正常工作。尽管在 C++03 中,字符串技术上不需要存储在连续的内存中。事实上,您会发现几乎所有
std::string
实现都会以这种方式存储它们,但这在技术上并不是必需的。 C++11 纠正了这个问题。因此,我建议您在这种情况下使用
std::vector
。std::string
不会为您购买任何超过std::vector
的东西,更明确的是,这是一个字符数组,而不是一个可能可打印的数组细绳。std::string
does not treat null characters specially, unless you don't give it an explicit string length. So your code will work fine.Although, in C++03, strings are technically not required to be stored in contiguous memory. Just about every
std::string
implementation you will find will in fact store them that way, but it is not technically required. C++11 rectifies this.So, I would suggest you use a
std::vector<char>
in this case.std::string
doesn't buy you anything over astd::vector<char>
, and it's more explicit that this is an array of characters and not a possibly printable string.我认为最好使用 char 数组 char[] 或 std::vector。这是保存图像的标准方法。当然,二进制文件可能包含0个字符。
I think it is better to use char array char[] or std::vector<char>. This is standard way to keep images. Of course, binary file may contain 0 characters.