Hacker News new | ask | show | jobs
by anc2020 5525 days ago
Sometimes you really do just want to pass a value by reference. It turns out that most of the time you want to do this the variable already is an object or array already, nevertheless there are cases where this would be desirable, so it's not a bad idea when used carefully.

Having said that, malloc is a bad idea and completely unnecessary, and a nicer way to do the functionality might be to do:

    function pmk(val)     { return { v: val }; }
    function pget(p)      { return p.v;        }
    function pset(p, val) { return p.v = val;  }
Doesn't need free'ing, at the minimal expense of creating one object per "pointer".

Neither of the solutions give you an "address of" type behaviour though :(