gpt4 book ai didi

c++ - 使用 std::string 作为缓冲区有缺点吗?

转载 作者:IT老高 更新时间:2023-10-28 12:10:35 27 4
gpt4 key购买 nike

我最近看到我的一个同事使用 std::string 作为缓冲区:

std::string receive_data(const Receiver& receiver) {
std::string buff;
int size = receiver.size();
if (size > 0) {
buff.resize(size);
const char* dst_ptr = buff.data();
const char* src_ptr = receiver.data();
memcpy((char*) dst_ptr, src_ptr, size);
}
return buff;
}

我猜这个人想利用返回字符串的自动销毁,所以他不必担心释放分配的缓冲区。

这对我来说有点奇怪,因为根据 cplusplus.com data() 方法返回一个 const char* 指向一个由字符串内部管理的缓冲区:

const char* data() const noexcept;

Memcpy-ing 到一个 const char 指针? AFAIK 只要我们知道自己在做什么,这并没有什么坏处,但是我错过了什么吗?这很危险吗?

最佳答案

不要使用 std::string作为缓冲区。

使用 std::string 是不好的做法作为缓冲区,有几个原因(不按特定顺序列出):

  • std::string不打算用作缓冲区;您需要仔细检查类的描述,以确保没有“陷阱”会阻止某些使用模式(或使它们触发未定义的行为)。
  • 作为一个具体的例子:在 C++17 之前,你 can't even write通过 data() 获得的指针- 它是 const Tchar * ;所以你的代码会导致未定义的行为。 (但 &(str[0])&(str.front())&(*(str.begin())) 可以。)
  • 使用 std::string s for buffers 让函数定义的读者感到困惑,他们认为您将使用 std::string对于,嗯,字符串。换句话说,这样做会破坏 Principle of Least Astonishment .
  • 更糟糕的是,这会让任何可能使用您的函数的人感到困惑——他们也可能认为您返回的是一个字符串,即有效的人类可读文本。
  • std::unique_ptr 适合您的情况,甚至 std::vector .在 C++17 中,您可以使用 std::byte 对于元素类型也是如此。更复杂的选项是具有 SSO 的类。 - 类似的功能,例如Boost的 small_vector (感谢@gast128 提及)。
  • (次要观点:)libstdc++ 必须将其 ABI 更改为 std::string为了符合 C++11 标准,所以在某些情况下(现在不太可能),您可能会遇到一些链接或运行时 issues你不会为你的缓冲区使用不同的类型。

此外,您的代码可能会进行两次而不是一次堆分配(取决于实现):一次是在字符串构造时,另一次是在 resize() 时。 ing。但这本身并不是回避 std::string 的真正理由。 ,因为您可以使用 @Jarod42's answer 中的构造来避免双重分配.

关于c++ - 使用 std::string 作为缓冲区有缺点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56422913/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com