- 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
void setFrist(int x) { x = FILE_NOT_FOUND; } /* TODO: optimize */
Admin
I have yet to see a function as clearly justifying the keyword "void" as this one.
Admin
But it's so optimal! The compiler will remove the function and calls to it as the function does nothing.
Admin
Really?
class Foo { public: Foo& operator=(const double t) {} };
#define int Foo&
void Bar(int x) { x = 0; }
Admin
It was cleared, if only for a moment before exiting or being optimized away.
Admin
Every comment atm is held for moderation. Clearly commenting has been implemented using this function. I bet this will also be moderated.
Admin
That function... could actually be clearing some data (or more specifically overwrite some junk memory) as a side-effect, as some stuffs would be written on the memory stack when the function is called (the value of some CPU registers would be saved there, and the memory for the local variable
x
would also be allocated there), and that would overwrite whatever data there was in the few bytes of memory involved.But if the goal is to reset a few bytes of memory, then that's an absolutely insane way to do it (and possibly unreliable and dependent on whether the compiler decides to nuke the function entirely for optimisation purpose since it doesn't do anything useful).
My guess is that it is something darker than just a very dirty hack. A tormented soul, scarred by the evil of a giant corporation, entered a mad trance, and unleashed on their keyboard every crazy raving that formed into their mind. Like bloodstains, many WTFs remained in the codebase, like tragic traces of the murder of the coder's sanity.
Admin
Nope. Or, if it was, it exited the queue before all the comments before it.
Admin
My first thought was, OK, this is a newbie who hasn't wrapped their head around "pass by value" versus "pass by reference". It can seem weird that modern languages like C# would take an integer by value, but an object instance by reference, even though the code looks the same in both cases.
But this is C, not C#, which doesn't have pass by reference (hence Remy's comment about using a pointer). Even C++ takes objects by value, unless specified otherwise, which in my opinion seems to be one of a number of mistakes that more modern languages learned from.
Admin
Paid by the line? I can call "clearVal" anywhere I want and not worry about it affecting the code. If I'm being paid by the line, then I'm going to call clearVal everywhere I can.
Admin
Maybe x was already 0 when mynameishidden posted the comment???
Admin
Schroedinger's Zero.
Admin
You have to be careful, because what is called a reference in C# is actually a pointer in C. A reference in C++ for example, is a pointer that has to be initialized and cannot be changed. So I don't know why "reference" was chosen for managed pointers in C#, I think it they followed Java and Java renamed 99% of terms for marketing reasons (like they called their interpreter VM, because interpreters were rightfully considered slow and inefficient), so I wouldn't be surprised that it makes actually no sense. Oh and the only difference between a managed and an unmanaged pointer is basically that an managed pointer includes a type reference to identify the object and is handled via garbage collection.
Admin
This actually doesn't seem so evil if you figure it once did something useful.
Admin
A reference in C++ is not a pointer. Neither syntactically (pointers must be explicitly dereferenced, references not), nor semantically (pointers can be null and can be rebound, references neither). References may be implemented via pointers internally, or not. E.g. if this function was void clearVal(int&x), any slightly optimizing compiler would inline all calls and do the increment on the actual parameter which may even be in a register; no pointers involved. In any case, it's an implementation details, just like pointers are in fact implemented as integers which contain memory addresses. Many educators now suggest teaching references early, and pointers only much later if at all to avoid this kind of confusion. Think of references as runtime-aliases for their referent.
Admin
You can call this function from multiple locations and place a breakpoint on that silly assignment.
Admin
That some poor noob wrote that isn't really a WTF. We all had to learn at some point and make stupid mistakes.
The TRWTF is that this was still in the codebase for someone to find. I have no idea how old is that codebase. It denotes a clear lack of peer review in check ins.
Admin
Maybe it was originally
and somebody looked at it, thought "this is wasteful and overly specific. Why are we passing in an entire object just to clear one if its values? If I change it to just take an int it can be reused for any object with an int peoperty"
Admin
"like they called their interpreter VM, because interpreters were rightfully considered slow and inefficient" : actually, no, the term was already in use for similar architecture (compilation to intermediate code, execution of the intermediate code by an interpreter or compiler). Search for p-code virtual machine.
Maurizio
Admin
I'm aware of the history; I lived through it. Back then the term virtual machine was exclusively used for, well, real virtual machines. And a p-code machine is not called virtual machine, just p-code machine. And that's nothing special, nor is intermediate code, every interpreter i know used them long before Java and no, nobody called them virtual machines before ;-).
Admin
Haha, rookie mistake. Everybody knows that you should always overwrite each memory cell three times to be safe!
The only correct way to do this is of course: x= (x = (x = 0))
Admin
<quote> It can seem weird that modern languages like C# would take an integer by value, but an object instance by reference</quote>
I don't know how to do quotes and this will probably never exit the moderation queue anyway but... Java passes by value only, and C# passes by value by default. When passing an object reference, the reference is passed by value, which trips some people up.
Admin
The behavior would be the same in C#. In .NET, "value types" (including primitives like int) are still passed by value, only "reference types" are passed by reference.