How to avoid a nightmare.... (String functions)

Started by
7 comments, last by Telastyn 19 years, 3 months ago
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...
chrString[strlen(chrString)] = 0;
chrString[strlen(chrString)-1] = 0;
chrString[strlen(chrString)-2] = 0;


Advertisement
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;
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Not using C strings would have made it even easier (If that was something available to you).
What if the string is less than 3 characters long? ...
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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! :)
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
Quote:Original post by Martee
What if the string is less than 3 characters long? ...


yap. fried.
[size="2"]I like the Walrus best.
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.
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.

This topic is closed to new replies.

Advertisement