C 更好地练习流和缓冲区
C++ Better Practice to Use of Streams and Buffers
我知道一种从流中阅读并使用下面使用的方法:
strstream s; // It can be another standard stream type
// ...
while (!s.eof())
{
char buf[MAX];
s.read(buf, sizeof (buf));
int count = s.gcount();
THIRD_PARTY_FUNCTION(buf, count);
// ...
}
但是此代码有一个虐待点,它首先将数据从流到buf
复制,然后将buf
传递给THIRD_PARTY_FUNCTION
。
有什么方法可以将代码改革以下(我的意思是下面的代码避免了额外的副本)?
strstream s; // It can be another standard stream type
// ...
while (!s.eof())
{
char *buf = A_POINTER_TO_DATA_OF_STREAM(s);
int count = AVAIABLE_DATA_SIZE_OF_STREAM(s);
// Maybe it needs s.seekg(...) here
THIRD_PARTY_FUNCTION(buf, count);
// ...
}
类似的东西可能对您有用。
char buffer[2000];
std::istream& s = getStreamReference();
s.rdbuf()->pubsetbuf(buffer, 2000);
while(s)
{
THIRD_PARTY_FUNCTION(buffer, s.rdbuf()->in_avail());
s.ignore(s.rdbuf()->in_avail());
// Not sure this may go into an infinite loop.
// Its late here so I have not tested it.
}
请注意,请确保我关心复制2K缓冲区的成本。
分析必须表明,这是一个真正的热点,在我考虑进行这种优化之前,在性能上造成了重大降解。在这里99%的时间里,可读性将是我最重要的因素。
您可以通过首先调用其成员方法str以获取std :: string,然后调用其构件函数C_ST它可以到c风格的无效终止char []。
相关文章:
- C++字符*缓冲区的大小
- 加速C++练习2.4
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- ostream过载时的缓冲区冲洗
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- Xaudio2在更改缓冲区或循环时弹出声音
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 多线程双缓冲区
- Android P-9.0.0_r53 Logcat主缓冲区超出定义大小
- 套接字读取后,我在缓冲区中看到意外输入
- std::带有自定义缓冲区的 iostream 不允许我写入
- 从返回的顶点缓冲区查询顶点结构
- Vulkan 中的动态顶点缓冲区格式设置
- OpenGL 16 位模板缓冲区?
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 在 openGL 中多次绑定缓冲区
- C 更好地练习流和缓冲区