sizeof in c++ showing string size one less

后端 未结 4 752
逝去的感伤
逝去的感伤 2020-11-30 15:44

In C++ I am trying to get size of a string say \"gibbs\";

When I am using sizeof function it is returning me size less then one of actual s

4条回答
  •  独厮守ぢ
    2020-11-30 16:19

    sizeof returns the size of the type of the object, std::string, which happens to be 4 on your system. This might be because the implementation of std::string you're dealing with uses the pimpl idiom -- i.e. it merely contains a pointer to the real implementation you're looking for, and pointers happen to be 32-bit in your host environment. It's more likely, though, that you're using a copy-on-write implementation of std::string, which no longer conforms to the specification as of the C++11 standard.

    std::string is defined to be a specialization of the std::basic_string template class, in particular std::basic_string; you should see the documentation on std::basic_string. take a look at either std::basic_string::size and std::basic_string::length.

    std::string s = "stackoverflow";
    std::assert(s.size() == 13);
    

提交回复
热议问题