I am wondering how come the sizeof function returned 8 no matter the length of my input
int main(){
string input;
getline(cin,input);
cout << "size of input is " << sizeof(input) << endl; //I am guessing
//it returns the size of a pointer because my OS is 64 bits.
return 0;
}
So my question is that where the implicit conversion happened? here is the declaration of getline,
istream& getline ( istream& is, string& str );
Also, this sort of conversion always happen, i.e from whatever to a pointer type, is there a general case for that? Thank you.
sizeofgives you the size in bytes.std::stringcontains a pointer to the actual data. The size of the single pointer stays the same no matter the size of what it’s pointing to. This, combined with the other factors gives you your total size of 8.You’re looking for either
std::string::sizeorstd::string::lengthfor the actual length of the string. If you’re looking for a function to retrieve the size of any null-terminated C-String, usestrlen().