IBM Support

LI75559: ISSUES WITH STL STD::STRING COPY ON WRITE

Subscribe

You can track all active APARs for this component.

 

APAR status

  • Closed as program error.

Error description

  • The following test case demonstrates an issue with the
    copy-on-write optimization performed by the string class:
    
    ===== COMPILE COMMAND:
    xlC test.cxx
    
    ===== TESTCASE:
    $ cat test.cxx
    #include <cstdio>
    #include <string>
    #include <vector>
    
    int main()
    {
      std::string const x = "x";
    
      int const n = 256;
      std::vector<std::string> v;
      v.reserve(n);
    
      for(int i=0; i < n; ++i) {
        v.push_back(x);
        std::string xy = x + "y";
        const char* p = x.c_str();
        std::printf("x = [%s], xy = [%s]", p, xy.c_str());
    #if defined(COUNT)
        std::printf(", count = %d", int(p[-1]));
    #endif
        std::printf("\n");
      }
    
      return 0;
    }
    
    
    ===== ACTUAL OUTPUT:
      $ xlC test.cxx && ./a.out
      x = [x], xy = [xy]
      ...
      x = [x], xy = [xy]
      x = [x], xy = [xy]
      x = [xy], xy = [xy]
      x = [xy], xy = [xy]
      ...
      x = [xy], xy = [xy]
      $
    
    ===== EXPECTED OUTPUT:
      $ xlC test.cxx && ./a.out
      x = [x], xy = [xy]
      ...
      x = [x], xy = [xy]
      x = [x], xy = [xy]
      x = [x], xy = [xy]
      x = [x], xy = [xy]
      ...
      x = [x], xy = [xy]
      $
    

Local fix

  • Use xlC_r instead of xlC, as xlC_r disables the copy-on-write
    optimization.
    

Problem summary

  • USERS AFFECTED:
    Users who use the same string many times in assign or append
    string operations may be affected by this issue.
    
    PROBLEM DESCRIPTION:
    When the same string is copied many times, the compiler uses
    the Copy On Write (COW) algorithm in the implementation,
    which only updates the reference count in the original string
    in the operation of assigning the original string to another
    string.
    When the reference count approaches to the maximum number for
    the string, instead of creating a copy of the original string,
    it reuses the original string, and when other operations like
    append happens, the original string also gets updated.
    This causes the test case start to fail after a certain number
    of copies.
    

Problem conclusion

  • The compiler has been changed to create a new copy of the
    original string, instead of reusing the string.
    

Temporary fix

Comments

APAR Information

  • APAR number

    LI75559

  • Reported component name

    XL C/C++ SLES10

  • Reported component ID

    5724U8300

  • Reported release

    A10

  • Status

    CLOSED PER

  • PE

    NoPE

  • HIPER

    NoHIPER

  • Special Attention

    NoSpecatt

  • Submitted date

    2010-07-28

  • Closed date

    2010-07-28

  • Last modified date

    2010-07-28

  • APAR is sysrouted FROM one or more of the following:

    IZ70285

  • APAR is sysrouted TO one or more of the following:

Fix information

  • Fixed component name

    XL C/C++ SLES10

  • Fixed component ID

    5724U8300

Applicable component levels

[{"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Product":{"code":"SSJT9L","label":"XL C\/C++"},"Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"A.1","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Document Information

Modified date:
17 October 2021