sizeof in c++ showing string size one less

后端 未结 4 741
逝去的感伤
逝去的感伤 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:11

    sizeof(s) gives you the size of the object s, not the length of the string stored in the object s.

    You need to write this:

    cout << s.size() << endl;
    

    Note that std::basic_string (and by extension std::string) has a size() member function. std::basic_string also has a length member function which returns same value as size(). So you could write this as well:

    cout << s.length() << endl;
    

    I personally prefer the size() member function, because the other containers from the standard library such as std::vector, std::list, std::map, and so on, have size() member functions but not length(). That is, size() is a uniform interface for the standard library container class templates. I don't need to remember it specifically for std::string (or any other container class template). The member function std::string::length() is a deviation in that sense.

    0 讨论(0)
  • 2020-11-30 16:18

    Because that's the size of the string type on your system, not the size of the underlying data. For example:

    #include <iostream>
    
    int main (void) {
        std::string s1 = "gibbs";
        std::string s2 = "a much longer string";
        std::cout << sizeof(s1) << '\n';
        std::cout << sizeof(s2) << '\n';
    
        std::cout << s1.length() << '\n';
        std::cout << s2.length() << '\n';
    
        return 0;
    }
    

    produces:

    4
    4
    5
    20
    

    In other words, you should be using std::string::length() to get the length of your strings.

    0 讨论(0)
  • 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<char>; 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);
    
    0 讨论(0)
  • 2020-11-30 16:27

    sizeof(s) in your example is returning the size of std::string, there is no relation to the content of what its members point to.

    How to fix this issue. We can add +1 always to return of sizeof but will that be perfect solution?

    no that won't solve anything because the value is constant, and sizeof is not providing the value you expect it does.

    if you want to know the number of characters required for the character sequence of the string, you can use s.length() or s.size() -- add 1 to that value to know the size required for a terminated string.

    also note that the value of sizeof is not going to be consistent across libraries and platforms. std::string implementations can vary greatly. in particular, you may see differences in pointer sizes and you may see small string optimizations.

    0 讨论(0)
提交回复
热议问题