0
Why does it output 12??
#include <iostream> #include <string> using namespace std; int main() { string a = "hello"; cout << sizeof (a) << endl; return 0; }
2 Answers
+ 5
C++ string is an instance of std::basic_string class, it's far more complex thing than C strings which are `char` arrays.
The class comes with self organized internal buffer which manages its work efficiently. How the buffer is allocated and number of bytes used is implemented as an algorithm in the class' allocator.
This is why `sizeof` operator isn't suitable for obtaining C++ string size, you should use either `size()` or `length()` method instead đ
+ 1
Thank you very much for the detailed answer. I was very stuck with this.