C++ 为什么不推荐使用 std::strstream?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2820221/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:10:23  来源:igfitidea点击:

Why was std::strstream deprecated?

c++stringstreamstrstream

提问by andand

I recently discovered that std::strstreamhas been deprecated in favor of std::stringstream. It's been a while since I've used it, but it did what I needed to do at the time, so was surprised to hear of its deprecation.

我最近发现,std::strstream有赞成被否决std::stringstream。我已经有一段时间没有使用它了,但它做了我当时需要做的事情,所以听到它被弃用时感到很惊讶。

My question is why was this decision made, and what benefits does std::stringstreamprovide that are absent from std::strstream?

我的问题是为什么做出这个决定,以及它std::stringstream提供了哪些没有的好处std::strstream

回答by

The strstreamreturned a char *that was very difficult to manage, as nowhere was it stated how it had been allocated. It was thus impossible to know if you should delete it or call free() on it or do something else entirely. About the only really satisfactory way to deallocate it was to hand it back to the strstreamvia the freeze()function. This was sufficiently non-obvious, that lots of people got it wrong. The stringstreamreturns a string object which manages itself, which is far less error prone.

strstream返回了char *,这是非常难以管理,因为无处它指出它是如何被分配。因此,不可能知道您是否应该删除它或对其调用 free() 或完全做其他事情。关于释放它的唯一真正令人满意的方法是将它交还给strstreamviafreeze()函数。这足够不明显,以至于很多人都弄错了。将stringstream返回一个String对象,其管理本身,这是远远不容易出错。

There was also the issue of having to use endsto terminate the string, but I believe the deallocation problem was the main reason for deprecation.

还有一个必须使用ends来终止字符串的问题,但我相信解除分配问题是弃用的主要原因。

回答by AProgrammer

Easier to understand memory management. (Can someone remember who is responsible for freeing the allocated memory and in which conditions?)

更容易理解内存管理。(有人记得谁负责释放分配的内存以及在什么情况下吗?)

(Note that as strstream still provide something which is not available elsewhere, it will continue to be present in C++0X -- at least last time I checked the draft it was).

(请注意,由于 strstream 仍然提供其他地方没有的东西,它会继续出现在 C++0X 中——至少我上次检查草稿时是这样)。

回答by Ken Bloom

A strstreambuilds a char *. A std::stringstreambuilds a std::string. I suppose strstreams are deprecated becuase of the potential for a buffer overflow, something that std::stringautomatically prevents.

Astrstream构建了一个char *. Astd::stringstream构建了一个std::string. 我想strstreams 已被弃用,因为缓冲区溢出的可能性会std::string自动阻止。

回答by Component 10

From a personal perspective on more than one occasion I've seen obscure memory corruptions that took days or weeks to track down and eventually came down to use of strstream. As soon as it was replaced with stringstreamthe corruptions vanished and I didn't ask any more questions! That was enough for me.

从个人的角度来看,我不止一次看到过模糊的内存损坏,这些损坏需要几天或几周的时间才能找到并最终归结为使用strstream. 一旦它被stringstream腐化所取代就消失了,我没有再问任何问题!这对我来说已经足够了。