C++ std::string 构造函数
对此的任何想法将不胜感激:
std::string s1 = "hello";
std::string s2 = std::string(s1);
我现在希望这两个字符串是独立的,即我可以将“, world”附加到 s2 并且 s1 仍然会读取“hello”。 这是我在 Windows 和 Linux 上找到的,但在 HP_UX 机器上运行代码似乎 s2 和 s1 是相同的字符串,因此修改 s2 会更改 s1。
这听起来绝对疯狂吗,有人见过类似的东西吗?
any thoughts on this would be appreciated:
std::string s1 = "hello";
std::string s2 = std::string(s1);
I'd now expect these two strings to be independent, i.e. I could append ", world" to s2 and s1 would still read "hello". This is what I find on windows and linux but running the code on a HP_UX machine it seems that s2 and s1 are the same string, so modifying s2 changes s1.
Does this sound absolutely crazy, anyone seen anything similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然我无法重现 OP 的确切错误,但我在 HP-UX aCC 编译器中遇到了类似的错误。 我在 HP 上发布了相关信息板,并最终得到了惠普的回应。 基本上,他们的 aCC 版本 3.xx(3.70、3.73、3.67 等)搞乱了 std::string 构造。 我们必须迁移到 6.xx 版本的编译器。 我们当时遇到的问题是,没有适用于 PA-RISC 机器的 6.xx 编译器,只有 Itanium。 我相信 2007 年 9 月发布了 PA-RISC 的 6.xx 编译器。
出现问题的代码是:
这是打印:
零=零
零=一,一=一
换句话说,从n1构造字符串1完全破坏了另一个字符串(字符串零)。
注意:
要查看编译器的版本,请输入“aCC -V”
要查看机器类型,请输入“uname -m”(9000/800 ==> PA-RISC,ia64 ==> Itanium)
Although I could not reproduce the exact bug of the OP, I came across a similar bug in the HP-UX aCC compilers. I posted about it on the HP boards, and eventually got a response from HP. Basically their versions 3.xx (3.70, 3.73, 3.67, etc.) of aCC have messed up std::string construction. We had to move to the 6.xx versions of the compiler. The problem we had at the time was that there was not a 6.xx compiler available for PA-RISC machines, just Itanium. I believe that a 6.xx compiler was released for PA-RISC in September 2007.
The code that was giving the problem was:
This was printing:
zero = zero
zero = one, one = one
In other words the construction of string one from n1 was clobbering another string completely (string zero).
NOTES:
To see the version of the compiler, type "aCC -V"
To see the type of machine, type "uname -m" (9000/800 ==> PA-RISC, ia64 ==> Itanium)
这一定是一个错误。 std::string 可以将引用计数字符串作为其实现,但是一旦它发生更改,它就应该“分叉”该字符串。
This must be a bug. std::string could do reference-counted strings as its implementation, but once it gets changed, it's supposed to "fork" the string.
这对我来说确实听起来像是一个错误。 其他有权访问 HP/UX 的人可以重现这个吗?
你是说这个程序在两行上显示相同的文本?
That sure sounds like a bug to me. Can anyone else who has access to HP/UX repro this?
You're saying that this program displays the same text on both lines?
这两个字符串实际上是否有不同的变化,或者您是否使用了其他比较(例如 c_str() 返回的地址)?
string 的某些实现在进行复制时不会复制整个字符串,以加快长字符串的复制速度。 如果您尝试对其中任何一个进行更改,则实现应复制该字符串并进行适当的更改(最好是作为同一操作的一部分)。
Do the two strings actually change differently, or are you using some other comparison (such as the addresses returned by c_str())?
Some implementations of string don't copy the entire string when a copy is made so as to speed up the copying of long strings. If you attempt to make a change to either one, then the implementation should copy the string and make the appropriate changes (ideally as part of the same operation).
这肯定是一个缺陷。 此示例逐字来自 C++ 标准:
It would certainly be a defect. This example is verbatim from the C++ standard: