3

So I thought I had a basic, high level understanding of C++ STL strings, pointers, copy constructors and stuff. In comes a dirname, a -D_GLIBCXX_USE_CXX11_ABI=0 and... Toto, I've a feeling we're not in Kansas anymore.
So what is happening? I copy a string expecting a deep copy, but then I do the dirname or manipulation on the copy and it messes up *both* strings. gcc/C++ I know you're a beast, but what's going on there? Thing is only possible if I cast away const from c_str - which of course is a doubtful operation - but there also seems to be some strange copy on write logic that the data pointers initially point to same memory location and only with first manipulation on the copy they start to point to different addresses.

I had no clue. And still don't have.

Comments
  • 5
    Casting away const means that half of your code thinks the variable cannot change (unless it's volatile const) and the other half does change it.
  • 0
    @Fast-Nop OK, so you kind of wreck the reasoning the compiler has (which is the types - and casting breaks that of course)... But still I hadn't guessed that could carry over from a copy.
  • 0
    Setting _GLIBCXX_USE_CXX_11_ABI to 0 changes the behavior of some classes to not conform to the C++11 standard anymore. One of those classes is std::string which was implemented as copy on write before C++11, but C++11(and newer standards) forbid this kind of behavior for std::string.
  • 0
    ..maybe just try it out yourself (only works with gcc, not clang, as far as I know)
Add Comment