Why sizeof() of a string variable always return the same number even when content changes?

前端 未结 3 770
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 05:23

This is a rather simple problem but is pretty confusing.

string R = \"hhhh\" ;
cout<< sizeof( R )<

OUTPUT:

4
         


        
3条回答
  •  深忆病人
    2021-01-26 06:07

    sizeof expression returns the size required for storage of the type expression evaluates to (see http://en.cppreference.com/w/cpp/language/sizeof). In case of std::string, this contains a pointer to the data (and possibly a buffer for small strings), but not the data itself, so it doesn't (and can't) depend on string length.

提交回复
热议问题