- 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
Will that even compile? Not on my compiler it wont!
In C++ free is templated, but it still expects a pointer as input... int n = 4; free(n); error C2664: 'free' : cannot convert parameter 1 from 'int' to 'void *'
This compiles though, but I expect it will crash: int n = 4; free(&n);
Admin
really now?
Admin
That is normal. The semi formal standard for C is to return 0 if there were no errors and an error number if there were errors.
Admin
This is why javac and compilers for many other languages die with a nice "error: unreachable code". I take it C compilers aren't that smart, or simply don't care? Does GCJ throw an error for unreachable code? (for Java I mean)
Admin
It should be pretty clear that all the free's are non-reachable code. Why aren't these lines removed? Not sure if deleting nodes during a traverse is going to cause a few minor problems perhaps? Nothing like manipulating a data structure without knowing what you are doing.
Captcha: onomatopoeia ... just for the sake of slowing down my typing. Or is it the sound of two hands typing.
Admin
I think I understand why you hated, I mean it is much better to use ++x
Admin
C programmers do not want compiler telling them what is right what is not. We usually have long memorized all of ANSI/ISO Standard becuase we can and also because we think that is a fundamental requirement that you know what you are doing. C is quite small compared to your Java/C++ and such Garbage (Collected) languages, and having the whole Standard in ones head is actually a good thing.
Admin
Do you know any C at all? I must say that the replies in today's submission show almost more incompetence than the submission itself! (More so than usually, anyway). Maybe I notice because it is written in a language I understand this time.
No, free(n) would not even compile.
Admin
And with that final comment, you point out that it's only the compiler that takes offense at such a line. As far as any compiled code is concerned, an int is just data and so is a pointer (though possibly of different lengths and with different alignment requirements, dependent on the architecture.)
Admin
int main(int argc, char** argv) { int n=0; free(n); return 0; } daid@slayer:~/test$ gcc -Wall -o main main.c main.c: In function
main': main.c:7: warning: passing arg 1 of
free' makes pointer from integer without a cast daid@slayer:~/test$ gcc -v Reading specs from /usr/lib/gcc-lib/i486-linux/3.3.5/specs Configured with: ../src/configure -v --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu --enable-debug --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc i486-linux Thread model: posix gcc version 3.3.5 (Debian 1:3.3.5-8ubuntu2.1) daid@slayer:~/test$gcc 3.3.5 says otherwise. (and it runs without crashing :P)
Admin
Not sure which is the biggest WTF here, the original code or some of the comments people have posted - I suppose it's a sign of the times that the kids today just don't know their C. Not that it was neccessarily any better in the good old days - I remember once pointing out to a colleague where he needed to stick a free() into his code to prevent a memory leak, and his response was basically "huh? I didn't know you had to do that". I think he ended up moving into sales.
Admin
It makes perfect sense Node *peer = "milton"; is not allocated on the heap so doing a free(peer) is indeed a big WTF
Admin
C compilers are smart, smart enough to know that unreachable code is an error on the part of the programmer and won't affect the program. Dying for semantics is useless, does JavaC die for this:
if (x = y) { /* Always execute */ }
Syntactically valid, semantically wrong, and actually more of a problem than dead code.
Plus (or minus, depending on your view of things), unreachable code may not always have to be unreachable in C. You can always go muck about with the instruction pointer if you really feel it is necessary.
The beauty of C is not its safety or ease of use, but its ability to do really complex stuff with no interference.
Admin
This looks like an attempt at Literate Programming.
Admin
i hate him too.
Admin
What kind a jackass tries to free an int? At least the code is dead but that still deserves a swift whack in the nuts with a baseball bat.
Admin
Actually, it does - if you are using a C compiler, not feeding C into a C++ compiler. Perhaps you should spend more time checking your facts and less time disparaging others.
Admin
Actually: if (x = y) { /* Execute unless y == 0 */ }
Admin
Perhaps all the calls to Free() are meant as political statements, not programming statements...it's more a vague yearning, not a requirement, which explains why he places the call to free after the return statement.
Admin
I love it! Excellent practice in writing intentionally unmaintainable code. Did he have anything explicitly against his fellow developers, or did he hate all mankind? ;)
Perhaps it would be even more evil to do:
That bug may remain hidden for a long long time...
Admin
"n = n;" IS NOT NECESSARILY DEAD code in C++...
In fact if those are arrays and "operator=" doesn't compare its argument to this, very bad things could happen.
Admin
Agree 100%. Just looking at the declaration of the function forewarns me it will be a WTF. Hadn't the guy heard of structs and handles?
Every first year CS student has to program tree creation, traversal, and destruction. This guy must have been sick that week.
Admin
But there are meaningful comments in the function ;-)
I think that Mr. Memory Leak will also find ways to write stupid Code in C#.NET and outwit the GC...
This guy will forget to implement IDisposable to free unmanged resources...
Admin
How about free(willie);
Sadly, he'd still be in his pool in Oregon as the code never gets executed. :(
Admin
Not quite -- in traditional (K&R) C, it's a Node*, and in ISO Standard C, it's a Node (*)[N] (pointer to N-element array of Node), where N is the number of elements in the initializer.
Admin
The "unreachable code" error is quite annoying. Imagine you need a quick way to stub some function in someone else's code for debugging purposes and it's riddled full of those /* */ style comments...
Admin
Or the people that came up with Symbian and Nokia for crapping up S60. You want to set the title of the window in Avkon? Ok, let's not use descriptors, since they would be evil. The called function would have to make a copy of the data it's given. That's not nice. Let's rather make it this way:
No deleting, nothing. If you do, the app will crash since the title pane is using the buffer. But surely if you change the title you must delete the old since you allocated it? Nope, Avkon will do that for you.
Great example of things going wrong. They probably hadn't heard about "allocated memory should never be freed in another DLL" and that kind of stuff either...
Admin
Immigration… [...]the time to read or visit the content or sites we have linked to below the[...]…group buy seo