My major achievement for today is writing the following five lines of code:
and having them do the wrong thing. One would normally expect this to print "AAB". But for me, right now, until I stop goofing off, it prints "ABB". Why is that?
Well, it's because my
malloc implementation looks like this:
This means that every time I allocate memory, I get the same pointer
back, and so
b will always be equal no matter what. And for
that matter any variable I create will always have the same value.
This is of course a terrible idea in real life, but it is really
Here's my real
malloc function (that causes the above code to print
"AAB", like it should):
The hardest part about this was not actually writing
malloc is easy, as long as you never need to free memory. I also
just wrote this by copying it from a C implementation. You just need
to keep a counter and keep incrementing it.
The hard part was getting the type of the function right, because Rust :). This is entirely made up for by being able to play silly memory games.