Skip to content

Instantly share code, notes, and snippets.

@amullins83
Created April 9, 2016 15:49
Show Gist options
  • Save amullins83/b712465983cdc3983ee8d27cc316dd71 to your computer and use it in GitHub Desktop.
Save amullins83/b712465983cdc3983ee8d27cc316dd71 to your computer and use it in GitHub Desktop.
sizeof(string) does not give you the string's length.
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main()
{
string sizeStr = "This is a test string that is rather long. Many bytes. Oh yes, quite a few bytes.";
cout << "The length of sizeStr is " << sizeStr.size() << endl; // The length of sizeStr is 81
cout << "sizeof(sizeStr) returns " << sizeof(sizeStr) << endl; // sizeof(sizeStr) returns 4
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment