Hello! Quick question here, since I haven't gotten any good C++ reference books yet.
Is the string length calculated each time string.length() is called, or is the value always kept "up-to-date" somewhere in the object?
Thanks!
-tretton
Hello! Quick question here, since I haven't gotten any good C++ reference books yet.
Is the string length calculated each time string.length() is called, or is the value always kept "up-to-date" somewhere in the object?
Thanks!
-tretton
well, AFAIK, that's really up to implementation, but you should probably figure that it's going to calcuate it whenever you call length().
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
A std::string is allowed to contains NULs, so it HAS to save the length.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Not necessarly -- capacity() is the number of byte the string class has allocated for the data. size() and length() are both the same and is the count of the number of characters up to the first 0. In the below example, the assignment operator changes the capacity to the number of characters including 0s in the entire string, while memcpy does not change the original allocated string's capacity.Originally Posted by CornedBee
outputCode:#include <iostream> #include <string> using namespace std; int main() { std::string str; str.resize(255); memcpy((char*)str.data(), "Hello\0World\0Now\0", 16); cout << "str.capacity() = " << str.capacity() << endl; cout << "str.size() = " << str.size() << endl; cout << "str.length() = " << str.length() << endl; cout << endl; str = "Hello\0World\0Now\0"; cout << "str.capacity() = " << str.capacity() << endl; cout << "str.size() = " << str.size() << endl; cout << "str.length() = " << str.length() << endl; return 0; }
[edit]A bit more testing shows that the assignment operator stops copying characters when it encounters the first 0 byte. So you can't use the assignment operator with strings that contain embedded null bytes.Code:str.capacity() = 255 str.size() = 255 str.length() = 255 str.capacity() = 31 str.size() = 5 str.length() = 5 Press any key to continue
Changing the first 0 to a space (or some other readable character) does not change the string's length(), so the value returned by length() is an internal class integer the represents the string's length only when the std::string's contents are changed by one of std::string's method, such as one of the operators. std::string does not perform strlen() each time length() is called. [/edit]
Last edited by Ancient Dragon; 01-07-2006 at 07:20 AM.
Thank you, everyone!
Corndbee is right in saying that a std::string is allowed to contain multiple NULL bytes. There is, however, an issue that, when given standard C-style strings, that std::string member functions (constructors, assignment, etc) only copy up to the first NULL byte. If you want to get multiple NULL bytes into a std::string, you have to jump through a few more hoops, and converting such a std::string back to a C-style string will mean you don't see all the contents of the std::string.Originally Posted by Ancient Dragon
As to whether that means that a std::string needs to store it's length, I'm not so sure, but suspect Corndbee is correct: by providing the data() method, the definition of std::string implicitly rules out implementations of std::string based on (say) a linked list of nodes, each containing a byte.
I wouldn't expect it to, memcpy() only works with raw memory, and does not cooperate if the memory it works with happens to belong to a std::string (or any other class type, for that matter).Originally Posted by Ancient Dragon
The snippet above would not change any of the members you are printing out. If you were to copy more than 255 bytes (or the string was of length 15 or less) the result would be undefined behaviour.Originally Posted by Ancient Dragon
That's the way it's supposed to be. But that doesn't mean a std::string cannot contain more than one null byte. It's just an artifact of how assignment to a C-style string works.Originally Posted by Ancient Dragon
This last observation is correct. However, try a test based on this;Originally Posted by Ancient Dragon
and you'll find the results are a bit different.Code:#include <iostream> #include <string> using namespace std; int main() { std::string str; char temp[] = "Hello\0World\0Now\0"; for (int i = 0; i < sizeof(temp); ++i) str.append(temp[i]); cout << "str.capacity() = " << str.capacity() << endl; cout << "str.size() = " << str.size() << endl; cout << "str.length() = " << str.length() << endl; return 0; }
grumpy: Where did I, or any else for that matter, say std::string could not contain more than one 0?
Agree -- more than 255 characters is simply C buffer overflow behavior, but I don't see where copying less than 15 characters would be a problem when the buffer will hold up to 255 characters.If you were to copy more than 255 bytes (or the string was of length 15 or less) the result would be undefined behaviour.
won't compile -- no append() overrides take char.Code:str.append(temp[i]);
That's what I get for typing too fast rom memory rather than having the standard at hand; one of many minor anomolies in the standard.Originally Posted by Ancient Dragon
Try
orCode:str.append(1, temp[i]);
Code:str += temp[i];
Peculiar indeed.Originally Posted by grumpy
You could use push_back() instead
Not on VC++6 with its original library, though. At least you need a STL replacement.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law