1

I have read a disk file into memory into an array declared as by:

char * buffer = new char [length];

then reinterpreted the array:

std::string strbuf(reinterpret_cast<const char *>(buffer), length);

and immediately check the type of the string provided.

cout << "buffer is: " << typeid(buffer).name() << '\n';
cout << "strbuf is: " << typeid(strbuf).name() << '\n';
buffer is: Pc
strbuf is: Ss */

As you can read, the string "strbuf" is of type Ss. What does that mean?

Steve Russell
  • 57
  • 1
  • 6
  • 1
    It's a mangled typename. See [this question](http://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname) – Barry Dec 22 '14 at 00:46
  • 2
    _One_ question per question, please. As should be self-evident. You wouldn't go to the Wikipedia article on "Dog" and expect to also find a description of cats. – Lightness Races in Orbit Dec 22 '14 at 00:50

2 Answers2

7

Ss is the mangled name for std::basic_string<char, std::char_traits<char>, std::allocator<char>> according to the Itanium ABI mangling rules.

If you have trouble applying the rules in your mind, you can use the c++filt tool:

$ c++filt <<< _ZSs
std::basic_string<char, std::char_traits<char>, std::allocator<char> >
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

This line:

std::string strbuf(reinterpret_cast<const char *>(buffer), length);

Which really should be:

std::string strbuf(buffer, length);

does not interpret buffer as a string. It copies it. strbuf at this point owns its own copy of the entire buffer. When you do delete [] buffer;, strbuf still has its own and is still a perfectly valid object. Since string manages its own memory, you don't need to explicitly delete it either - you can just let it go out of scope.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • Ok - I see now why only one question in each post. Both my questions were answered but by different posters. I can only accept one post as the answer though, so one good answer does not get recognized. – Steve Russell Dec 22 '14 at 19:00