chrString[strlen(chrString)] = 0;
chrString[strlen(chrString)-1] = 0;
chrString[strlen(chrString)-2] = 0;
How to avoid a nightmare.... (String functions)
How to avoid a nightmare? Watchout for things like this...
This mistake cost me 4 days of debugging (Though I found other mistakes in my code, it would have been faster to correct them later on.).
This code knocks off the last 3 characters in a C-style string, or does it...
Solution();
/* Scroll down to reveal the solution to this code! */chrString[strlen(chrString)-1] = 0;chrString[strlen(chrString)-1] = 0;chrString[strlen(chrString)-1] = 0;
Although strlen is a fast function, why call it three times when you only have to call it once?
int len = strlen(chrString);
chrString[len] = 0;
chrString[len-1] = 0;
chrString[len-2] = 0;
and moreover, if the point is to cut the last 3 characters, why not jump straight to the cut off point:
chrString[strlen(chrString)-2] = 0;
int len = strlen(chrString);
chrString[len] = 0;
chrString[len-1] = 0;
chrString[len-2] = 0;
and moreover, if the point is to cut the last 3 characters, why not jump straight to the cut off point:
chrString[strlen(chrString)-2] = 0;
Not using C strings would have made it even easier (If that was something available to you).
Quote:Original post by ProcedureX
This code knocks off the last 3 characters in a C-style string, or does it...
[ Solution omitted. ]
No, it doesn't. It knocks off the last 3 characters in any string of at least 3 characters. In a string of 2 or fewer characters, you're probably going to be doing an illegal write and getting a runtime exception somewhere.
Use std::string. It just wants to be loved! :)
For once I'd like a thread where I wasn't recommended to use C++... [bawling]
Anyways, there are a few assumptions I didn't include in the code sample. One of them, which I see brought some critism, was the assumption that the string is larger than 3 characters.
[google] "A forum for procedural programmers" [google]
Cya all.
Anyways, there are a few assumptions I didn't include in the code sample. One of them, which I see brought some critism, was the assumption that the string is larger than 3 characters.
[google] "A forum for procedural programmers" [google]
Cya all.
Heh, that's an amusing bug.
Though I'm not exactly certain how std::string would prevent that sort of bug [the bug that exists for strings greater than 3 chars that is...]
Personally, if you're overwriting a block of data like that, using pointers and bzero/memset is more... C-style. Plus it gets you as the programmer thinking in blocks of memory rather than in words and letters, preventing such oopsies.
Though I'm not exactly certain how std::string would prevent that sort of bug [the bug that exists for strings greater than 3 chars that is...]
Personally, if you're overwriting a block of data like that, using pointers and bzero/memset is more... C-style. Plus it gets you as the programmer thinking in blocks of memory rather than in words and letters, preventing such oopsies.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement