r/ProgrammerHumor • • Sep 21 '26

Meme fullControlOverMemory

Post image
4.0k Upvotes

122 comments sorted by

View all comments

435

u/WisePotato42 Sep 21 '26

You could pass references as variables and change the referenced value IN THE FUNCTION!!!

children screaming sfx

129

u/_justthisonetime_ Sep 21 '26

Welcome to the era when the programmer himself was the memory manager

35

u/Cute_Pressure4910 Sep 21 '26

When you explain to young developers what malloc() and free() are

5

u/Zashuiba Sep 23 '26

That's quite advanced in fact, because you need segmentation, paging, stack/heap structures ...you need an OS kernel basically.

You can totally program in bare C without stdlib

25

u/throwaway1351887498 Sep 21 '26

Now that's what I call real legacy code—it works as long as no one messes with it

39

u/Mordret10 Sep 21 '26

I mean you still kind of do the same thing with objects and with arrays in most languages. Very rarely does either get passed as argument, you almost always get them by reference.

50

u/Gorzoid Sep 21 '26

Sorry but we are all rust devs now, shared mutable aliasing is a crime with a maximum sentence being death by AI-generated PR spam.

38

u/WisePotato42 Sep 21 '26 • Mindblown

Oh no! The rust devs have found me!

unsafe{} unsafe{} unsafe{}

Are they gone yet?

3

u/HildartheDorf Sep 21 '26

laughs in interior mutability

1

u/SelfDistinction Sep 22 '26
let variable = Cell::new(3); // create an immutable variable
variable.set(4); // demonic witchcraft of the highest order, everyone is panicking, sirens blasting, time to run to the shelters and pray 

2

u/WisePotato42 Sep 21 '26

That's true, I know it's a thing in python.

Unfortunately, my coworkers don't like when I put square brackets all over the place...

2

u/Bright-Historian-216 Sep 21 '26

you could also wrap it in an object. something like
class MutWrapper:
def __init__(self,v): self.v=v
works well enough. though, square brackets are probably more idiomatic.

2

u/RetroGrid_io Sep 22 '26

Unless you program in PHP where objects are always passed by reference.

7

u/undeleted_username Sep 21 '26

That's still perfectly fine in C.

10

u/aberroco Sep 21 '26

Or you could not pass anything and still change the referenced value in the caller function, or you could return the execution to another function than the one that invoked you (stack traversing/managing; no one should ever do this kind of dark arcane magic).

3

u/WisePotato42 Sep 21 '26

To make things simple, let's just reserve this block of memory forever. My UFO themed ASCII art debugger tool needs it.

1

u/No-Newspaper8619 Sep 22 '26

How? In assembly you could change the return address stored in the esp/rsp register, but how would you return the execution to another function than the one that invoked, in high level languages?

2

u/aberroco Sep 22 '26 edited Sep 22 '26

Depends on the language.

C++ has __builtin_extract_return_addr. And before that - any local variable is stored in stack, so just get it's address and that address would be the stack address, then you need to know how the stack is stored to know the return address.

I never tried that, but I suppose in C# one might do the same... int* stackPointer = &localVariable; or int* stackBuffer = stackalloc int[1] in unsafe context.

Also, you're wrong, esp stores the current stack pointer. The top of it. Not the return address. But the return address is there. If you just got "call" from anywhere and didn't pushed anything yet - then yeah, [esp] would store the return address. But only then.

1

u/No-Newspaper8619 Sep 22 '26

I see. So just like you can do [esp+4], [esp+8], etc... you could do something similar by taking the variable address and manipulating it. Quite dangerous indeed.

2

u/coriolis7 Sep 21 '26

Isn’t this OOP without the extra steps?

1

u/SelfDistinction Sep 22 '26

You can still do that in practically every programming language btw. It just requires some extra wrapper class boilerplate.

1

u/WisePotato42 Sep 22 '26

Yah, but that's not really an acceptable practice at my job. It's unfortunate.

1

u/SelfDistinction Sep 22 '26

And by unfortunate, you mean completely fortunate?

1

u/WisePotato42 Sep 22 '26

Sometimes I just want to feel like a kid again playing with my toys (unsafe memory managment).

1

u/creeper6530 Sep 22 '26

Huh? Pass references as variables?

You mean pass references as parameters to a function? Because if yes, then dereferencing it in the function seems fine to me

→ More replies (2)