- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
e.g. This might even compile, but its not allowed: char* bob = "bob"; bob[0] = 'j';
after this bob might point to "job", but it might still point to "bob", or it might even throw an exception, or just crash - that's the thing about undefined behaviour...
Admin
'Allowed'? 'Allowed'?? This is C, for heaven's sake! If you can't go in and modify a byte because you just know what that byte should be, why are you programming in C in the first place? The point is that people can, and do, write to string literals in C. They use them as handy buffers. To pad to 30 spaces wide, they declare a literal string of 30 spaces and then sprintf into it, and it works perfectly! Almost every time!
To heck with this silliness of 'allowed'! In C, it's all about what you CAN DO!
Now, if you'll excuse me, I'm going to create a loop by simply having a bit of code that modifies CS:IP directly! Because I CAN!
Naw, I'm kidding. I'm going to write some C#.
Admin
I don't see the WTF. Obviously whoever programmed this needed optimized access to strings that are N spaces. That is no WTF by itself, so how would you do it?
Keep in mind that in native C++ you cannot have a function that dynamically allocates a string buffer without requiring the user of said function deallocate it (and doing that would be a true WTF).
This array of N-spaced strings works like a function (with a max limit, but there might not be a need for more). Now, in C# or any garbage collected environment this would be a WTF, but not in C++.
Admin
the problem is that some compilers will optimize same string literals in to one instance of that string, which means that if you have two different peices of code depending on a literal, and you change one, the other will be change as well.
before this part of the program, the buffer (pDuration) is already allocated, which means that the char array waste of space and the char array also can also have a possible problem of buffer overflow/underflow.
Admin
I've far too much time on my hands. Anyone want to expand this for subtraction, multiplication and some floating point?
char *pSpaceChar[10] = {
//Oli " ", // EightSpace Characters
char del="#";
int spacetoint(char *a) { int num=0, pow=1; for (int i=strlen(a); i>0; i--) { if (a[i]!=del) continue; for (int j=9; j>0; j++) if (strcmp(a+i, pSpaceChar[j])==0) { num += (strlen(pSpaceChar[j])*pow) pow++; } } return num; }
char *inttospace(int x) { char *str; int pow=1, fig;
}
char *add(char *a, char *b) { int j = spacetoint(a) + spacetoint(b);
return inttospace(j); }
Admin
Oh dear does nobody get the fact that (s)printf already has formatting to pad with spaces. I guess there are not enough C programmers here.
By the way, const has been part of the ANSI-C standard for a long time and you need 2 consts here to define actual constants thus:
With only the first const you would not be able to modify the strings but you could modify the array to contain different literals / non-writable strings.
It's also probably another issue of the root of all evil, premature optimisation.
Admin
See this example for proper use of the MAYBE construct:
Admin
He is already using printf. So why gum up the works by adding random strings? This isn't any more optimized that using printf for what it is designed to do. As I mentioned before, if the "optimized" string stradles a page, you'll just blow away any sort of performance you gain.
And besides what would you rather maintain? Don't reinvent the wheel unless you have a really good reason.
Admin
uhh, arent std::string and std::auto_ptr part of any native c++ implementation, and dont they allow you to do just that?
If you're not using the STL in C++, then you're not writing C++ code, you're writing "C, with classes" code.
Admin
You're right, the need to call delete can be solved using auto_ptr. But using this approach has the negative of having to reallocate the space character string every time it's asked for (since it's deallocated every time).
You would also have to wrap them in a class, since auto_ptr calls delete and not delete[]. Maybe there is an equivalent for delete[]? I don't know.
Still don't think this is enough to warrant a WTF. Even if he's using printf as someone mentioned above. We know nothing about the requirements for optimized code in this example.
Admin
Come to think of it, the guy above is right about the printf thing. He's already using it, so the optimization makes no sense here.
See, I can admit when I'm wrong. That's why I'm better than all of you. (J/K)
Admin
public static string PadSpaceRight(string original, int size) { if (size < 0 || size > 7) return "You're sh*t out of luck!"; return string.Format("{0}{1}", original, pSpaceChar(size); }
... PadSpaceLeft and so on...
ironically - CAPTCHA: muhahaha
Admin
If this code is in a mission critical performance point, it's actually the way to do it. Nothing to see here, move along, ofcourse you should go for puts and not sprintf in that case.
And if any of you really think that those places should have a for loop (insane for performance) or a separate function(are you completely nuts ?) , you should go back to programming a-b-c.
The code is not a beauty, but performance has its cost.