C++ std::string 构造函数

发布于 2024-07-06 06:59:21 字数 282 浏览 7 评论 0原文

对此的任何想法将不胜感激:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感受沵的脚步 2024-07-13 06:59:21

虽然我无法重现 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 编译器。

出现问题的代码是:

#include <iostream>
#include <string>

class S : public std::string  // An extension of std::string
{
public:
  explicit S(const char* s)
    : std::string(s)
  {
  }
};

class N     // Wraps an int
{
public:
  explicit N(int n)
    : _n(n)
  {}
  operator S() const   // Converts to a string extension
  {
    return _n == 0 ? S("zero") : (_n == 1 ? S("one") : S("other"));
  }
private:
  int _n;
};

int main(int, char**)
{
  N n0 = N(0);
  N n1 = N(1);

  std::string zero = n0;
  std::cout << "zero = " << zero << std::endl;
  std::string one = n1;
  std::cout << "zero = " << zero
            << ", one = " << one << std::endl;

  return 0;
}

这是打印:
零=零
零=一,一=一

换句话说,从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:

#include <iostream>
#include <string>

class S : public std::string  // An extension of std::string
{
public:
  explicit S(const char* s)
    : std::string(s)
  {
  }
};

class N     // Wraps an int
{
public:
  explicit N(int n)
    : _n(n)
  {}
  operator S() const   // Converts to a string extension
  {
    return _n == 0 ? S("zero") : (_n == 1 ? S("one") : S("other"));
  }
private:
  int _n;
};

int main(int, char**)
{
  N n0 = N(0);
  N n1 = N(1);

  std::string zero = n0;
  std::cout << "zero = " << zero << std::endl;
  std::string one = n1;
  std::cout << "zero = " << zero
            << ", one = " << one << std::endl;

  return 0;
}

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)

哭了丶谁疼 2024-07-13 06:59:21

这一定是一个错误。 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.

雪花飘飘的天空 2024-07-13 06:59:21

这对我来说确实听起来像是一个错误。 其他有权访问 HP/UX 的人可以重现这个吗?

你是说这个程序在两行上显示相同的文本?

#include <stdio.h>
#include <string>

int main () 
{
    std::string s1 = "hello"; 
    std::string s2 = std::string(s1);   // note: std::string s2( s1); would reduce the number of copy ctor calls

    s2.append( ", world");

    printf( "%s\n", s1.c_str());
    printf( "%s\n", s2.c_str());
}

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?

#include <stdio.h>
#include <string>

int main () 
{
    std::string s1 = "hello"; 
    std::string s2 = std::string(s1);   // note: std::string s2( s1); would reduce the number of copy ctor calls

    s2.append( ", world");

    printf( "%s\n", s1.c_str());
    printf( "%s\n", s2.c_str());
}
虚拟世界 2024-07-13 06:59:21

这两个字符串实际上是否有不同的变化,或者您是否使用了其他比较(例如 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).

一身骄傲 2024-07-13 06:59:21

这肯定是一个缺陷。 此示例逐字来自 C++ 标准:

string s1("abc");
string::iterator i = s1.begin();
string s2 = s1;
*i = ’a’;  // Must modify only s1

It would certainly be a defect. This example is verbatim from the C++ standard:

string s1("abc");
string::iterator i = s1.begin();
string s2 = s1;
*i = ’a’;  // Must modify only s1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文