• Officer Johnny Holzkopf (unregistered)

    void setFrist(int x) { x = FILE_NOT_FOUND; } /* TODO: optimize */

  • akozakie (unregistered)

    I have yet to see a function as clearly justifying the keyword "void" as this one.

  • Bob (unregistered)

    But it's so optimal! The compiler will remove the function and calls to it as the function does nothing.

  • TheCPUWizard (unregistered)

    Really?

    class Foo { public: Foo& operator=(const double t) {} };

    #define int Foo&

    void Bar(int x) { x = 0; }

  • Registered (unregistered)

    It was cleared, if only for a moment before exiting or being optimized away.

  • (nodebb)

    Every comment atm is held for moderation. Clearly commenting has been implemented using this function. I bet this will also be moderated.

  • Sauron (unregistered)

    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.

  • (nodebb) in reply to mynameishidden

    I bet this will also be moderated.

    Nope. Or, if it was, it exited the queue before all the comments before it.

  • (nodebb)

    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.

  • Dr, Pepper (unregistered)

    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.

  • (nodebb) in reply to jeremypnet

    Nope. Or, if it was, it exited the queue before all the comments before it.

    Maybe x was already 0 when mynameishidden posted the comment???

  • Duston (unregistered)
    Comment held for moderation.
  • Oracles (unregistered)
    Comment held for moderation.
  • Randal L. Schwartz (github) in reply to Registered

    Schroedinger's Zero.

  • Foo AKA Fooo (unregistered) in reply to prueg
    Comment held for moderation.
  • (nodebb) in reply to prueg

    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.

  • (nodebb)

    This actually doesn't seem so evil if you figure it once did something useful.

  • Foo AKA Fooo (unregistered) in reply to MaxiTB

    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.

  • Ulli (unregistered)

    You can call this function from multiple locations and place a breakpoint on that silly assignment.

  • (nodebb)

    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.

  • Jaloopa (unregistered)

    Maybe it was originally

    void clearVal(MyObject x) {
            x.SomeInt = 0;
    }
    

    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"

  • Maurizio (unregistered) in reply to MaxiTB

    "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

  • (nodebb) in reply to Maurizio

    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 ;-).

  • Iterator (unregistered)

    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))

  • (nodebb) in reply to prueg

    <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.

  • Craig (unregistered) in reply to prueg

    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.

Leave a comment on “Clear This”

Log In or post as a guest

Replying to comment #:

« Return to Article