- 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
/* a marriage of 2 woefully bad concepts and an expansion of bad coding practice. This doesn't deserve to live near well-written code so we must continue the trend. Obviously we should use a nice, long switch to generate the required number of spaces to indent the paragraph... ;) */
Admin
this one goes with the christmas tree!!!
captcha = smile ... SMILE! It's CHRISTMAS in February
Admin
The real WTF is that you don't need separate strings, the C language is perfectly happy if you overlap them:
char Spaces[] = " ";
#define GimmeSpaces(HowMany) Spaces[ sizeof( Spaces ) - HowMany );
Admin
#include <stdio.h> #include <stdbool.h>
char *pSpaceChar[8] = { "", // No Space Characters " ", // One Space Character " ", // Two Space Characters " ", // Three Space Characters " ", // Four Space Characters " ", // Five Space Characters " ", // Six Space Characters " " }; // Seven Space Characters
int back=0 ; int forth=1 ;
int main(void) { int way=forth ; int i=-1 ; while(true) { if(way) ++i ; else --i ; printf("%sWTF!!!\n", pSpaceChar[i]) ; if (i==0) way=forth ; else if (i==7) way=back ; } }
Admin
kilroy was here
captcha = kungfu .. kilroy and kungfu have the same number of characters and both start with k
Admin
Okay, I'm going to start a new rabbit trail here. Years ago I was using a very brain-dead C compiler to develop embedded code for a product that used the Motorola 6809 processor (8-bit with with 64 kbyte max address space). Running out of code space (i.e. EPROM), I went searching for ways to optimize for space. I discovered much of my precious code space being occupied by long (32-bit) constants with values of zero, one, and two. So, I gave the compiler a bit of help by defining
const long ZERO = 0; const long ONE = 1; const long TWO = 2;
and then using them in place of the literal values. This change alone freed up over a kbyte of code space.
I can easily see pSpaceChar being used for the same purpose.
Admin
stdin:3: error: `eightSpaces' undeclared (first use in this function) stdin:3: error: (Each undeclared identifier is reported only once stdin:3: error: for each function it appears in.)
Admin
'rap'. Not 'bad wrap'. A bad wrap is a burrito with salad dressing.
Admin
Admin
I also hate the pointless comments for the code. I bet the person then failed to comment some other, more complicated sections of code.
Admin
Obviously, this was done so that the spaces could be localized properly for other cultures. If you didn't realize that immediately, you're an insensitive bastard.
Think globally, act ridiculously.
Admin
Very large Atari BASIC programs used to use the same tricks. A token for a variable took up less space than a constant, so at the beginning of the program, you'd see something like this:
100 N0=0: N1=1: N2=2: N3=3: N4=4
. . .
190 N100=N10*N10
etc. Those variables would then be used for calculations. Atari BASIC had a limit of 128 variables, so you could get quite a few common numbers replaced with variables and still have enough left over for the actual program.
Admin
xmas tree wins
Admin
Admin
Yup, all numbers in Atari 8K Basic were 6-byte floats, vs 1 (2?) bytes to reference a variable. I think it might have also been faster to reference numbers this way, but don't quote me on that.
Atari Basic was pretty amazing for its time. Sure there weren't any string arrays, but any one string could be as large as memory!
Admin
Could be a poor way to make fixed width values for a flat file instead of using sprintf to do it for ya.
Admin
Actually, I think this Captcha thing started with this Pop-up Potpourri:
http://thedailywtf.com/Articles/Pop-up_Potpourri_0x3a__Announced_By_God.aspx
Loook one of the latest
Admin
Admin
Where is the WTF? This array is useful when you frequently have to write certain amounts of blanks, e.g. when you want to indent each output line.
Admin
I am afraid I feel to see the wtf in the array of spaces. But if anyone can show me a way to get such strings of spaces (for some applications you could need quite a few very often) without having to assemble them at runtime, Im willing to learn...
Admin
sprintf(mystring,"%7s",otherstring);
This will pad any string with up to 7 spaces (on the left). Use -7 instead if you want the padding on the right.
Admin
Obviously the way to use this array is:
char *pSpaceChar[8] = { "", // No Space Characters " ", // One Space Character " ", // Two Space Characters " ", // Three Space Characters " ", // Four Space Characters " ", // Five Space Characters " ", // Six Space Characters " " }; // Seven Space Characters
int getPadding(char *mystring){ for(int i=0; i<8; i++){ sprintf(test1,"%7s",mystring); sprintf(test2,"%s%s",pSpaceChar[i],mystring); if (strcmp(test1,test2) == 0) return i; } return -1; }
int main(){ char *mystring = "asdf!"; printf("%s%s",pSpaceChar[getPadding(mystring)],mystring); return 0; }
Admin
Addition!
Admin
Admin
I know! The initialisation is the real WTF! It should look like this:
unsigned long long int nspaces[8]= { 0LL, 32LL, 8224LL, 2105376LL, 538976288LL, 137977929760LL, 35322350018592LL, 9042521604759584LL };
printf("%s", &nspaces[i]);
Admin
By using pointer arithmetic, one doesn't need all the values of the array, just the last one.
Admin
Moreover the original snippet tried to align the text to the CENTER, which probably is hard to do using "+9s" or ".9s" or just "9s" which many of you seemed to miss. In fact it failed to do so, because it should have been (9-iLength)/2 and not (9-iLength)*2.
I think it should have been: sprintf(pDuration,"%*s%s", (9-iLength)/2,"", pPointer);
Admin
My C is a bit rusty (okay, extremely rusty) so you'll have to excuse me if this is hideously incorrect, but I'll give it a go anyway, designing for maximum inefficiency of course:
(Please note that my never saving the value of strlen(s) is also intentional!)
Admin
char pSpaceChar=" "; / 7 spaces */
/* Adjust for decimal point */ sprintf(pDuration, "%s%s", &pSpaceChar[(9-iLength) * 2], pPointer);
If you know that the string is stored as a sequence of spaces terminated by a null, then you can see that by pointing at different starting point of the strings would give you a different length.
This is a shorter and faster solution than a for-loop and is closest to the intention of the original code.
Admin
If you want to save at runtime, use a single character string and take a slice of it (as described above).
Or .. I guess .. you could ..
Admin
Same on a ZX Spectrum. Number representation expanded numbers (don't remember exact byte count), while builtin constants such as
were tokenized to single bytes. So space-constrained BASIC code had plenty of constant expressions like and which still took way less bytes than storing a literal zero or one. Don't remember how much the calculations cost.Admin
My C is a bit rusty (downright decripit) but wouldn't something like this do exactly the same thing as the original code?
Or maybe even a one-liner like this?
Admin
Admin
Is it? Do you have any evidence that sprintf's time complexity is scaling with the number of spaces you want? How is copying 66 characters from an array any fast than copy one character 66 times? In either case you have to "assemble" it at runtime. And in both cases you are already calling sprintf. Why not use sprintf for what it is built for (formatting strings)???
Admin
class SpaceChar { public: const char* operator[] (size_t n) {
};
SpaceChar pSpaceChar;
Admin
Of course, for maximum clarity, it should have been written:
sprintf(pDuration, "%s%s", ((9-iLength) * 2)[pSpaceChar], pPointer);
Admin
I had to use something like this once to format text. A program we interfaced with took exactly 4 characters for a message. The message could, however, be less than 4 characters. The rest had to be padded with spaces.
So I don't really understand what the problem with this code is necessarily.
Admin
the funny thing is that people haven't realized is that if iLength <= 4 or iLength > 9, it will fail.
Is it just me or is it that there are more people who don't have a clue what there doing.
anyway just a suggestion (I know that this does not exactly fit problem, but think about this. If this were to be printed to the screen, the original line wouldn't line up all of the time): sprintf(pDuration, "%18s", pPointer);
Admin
If the spaces are being used for padding, then the number of spaces to use has to be computed at runtime anyway (since you don't know how wide the value you'll be printing is). Now, if you have some information that allows you to compute this width without calling strlen(), then you might be able to do it in fewer operations than sprintf() (which has to calculate the length of the string in order to properly pad it).
But I assert that it doesn't practically matter. It's O(n) in either case, since you still have to PRINT EVERY CHARACTER which is O(n) in the number of characters, so any optimization you make cannot make it less than O(n).
If the computation of the string length is truly such a bottleneck, it's probably time to step back and reexamine just what the hell is going on. Something is probably fundamentally wrong somewhere.
Admin
Yeah, I was trying to convince myself that this was some sort of "wrapper" joke. But no, it's just...illiteracy.
...phsiii
Admin
Well, I can't aggree on this - but University is looong over, so maybe it's because of this :-)
1.) O(): As much as I remember and believe, O() is perfect for choosing the right /algorithm/... e.g. in the rule-of-thumb "O(1) is better than O(n)"... Well, it's a perfect rule of thumb :-)
When you've found the correct algorithm - it's time to go to the /implementation/. And this one /does/ practically matter - at least sometimes :-)
Take to functions with both O(n):
and compare it to this one:
Both have O(n), same results - but (how amazing) walltime differs :-)
Back to the topic: the prebuilt space-string is just a form of loop-Unrolling. A stupid one (bad implemented), but it's a valid one.
You could also say
is [at least nowadays] completely stupid compared to
And I'd aggree as a rule of thumb to this. Actually, you /really/ have often to "loop-unroll" manually in the source (Yes, It's a wtf in itself) /nowadays/ - e.g. if you write "realtime" java-games for mobile Phones with J2ME.
So there are even nowadays many applications, that need those "dirty tricks" - so I can't aggree on
Admin
CAPTCHA: Pinball; I could do with some... starts MS Pinball
Admin
Well you could make snow to go with the Christmas tree:
Admin
'rep', actually. It's short for 'reputation'. Unless you're talking about 50 cent or the like. That is bad rap.
Admin
int a, b, c; char buf[MAXNUM]; a = 1; b = 2; c = strlen(strcat(strcpy(buf, pSpaceChar[a]), pSpaceChar[b]));
The rest is left for the reader -- this code hasn't been tested.
Admin
My very first program, written in BASIC for the Boy Scout computer merit badge, consisted of something like this:
10 PRINT "XOOOOOOO" 20 PRINT "OXOOOOOO" 30 PRINT "OOXOOOOO" 40 PRINT "OOOXOOOO" 50 PRINT "OOOOXOOO" 60 PRINT "OOOOOXOO" 70 PRINT "OOOOOOXO" 80 PRINT "OOOOOOOX" 90 PRINT "OOOOOOXO" 100 PRINT "OOOOOXOO" 110 PRINT "OOOOXOOO" 120 PRINT "OOOXOOOO" 130 PRINT "OOXOOOO" 140 PRINT "OXOOOOOO" 150 GOTO 10
(No idea if that's the right syntax for Basic, I haven't used Basic since then)
I believe I called it "screensaver" or something. Somehow my merit badge advisor let me count it.
Anyway, this snippet could be used for my awesome screensaver program...
Admin
How is this loop unrolling? Passing in a prebuilt space string is akin to: while (*dst++ = *src++); (two increments, a copy, and a compare) And the formatted way is akin to: for(i = 0; i < N; i++) *dst++ = ' '; (two increments, a copy, and a compare)
BOTH require loops. One is simpler and easier to maintain.
Oh and the stupid moronic "loop unrolling" has the potential to cause a page fault as the prebuilt string of spaces might lay across a page boundary.
Admin
Psch, no one's used abused C++ with overly verbose and redundant OOP-programming.
Belongs more or less in a obfuscation contest, but eh, it's my shot at it. [Yeah, on the preview, the text editor started to screw up]
I'd try to make it compile (except for the miscellaneous exception types), but I don't have enough time.
Admin
I beg your pardon?
the obvious truth about global variables aside - there are times when I do not get your native speaker's humor... I'd think that this should read "bad rep" (perhaps even "bad rap" would make some sense), but "wrap"??
I can't wrap (!) my head around that, sorry...
Admin
CAPTCHA: Wow this captcha is tangentially related to the current conversation!! It's almost as if someone made the captcha based on frequent topics of discussion on this website!!! omg so crazy
and btw mine was WAFFLES! can you believe that!