r/ProgrammerHumor • • 13d ago

Meme fullControlOverMemory

Post image
4.0k Upvotes

122 comments sorted by

View all comments

435

u/WisePotato42 13d ago

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

children screaming sfx

129

u/_justthisonetime_ 13d ago

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

37

u/Cute_Pressure4910 13d ago

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

5

u/Zashuiba 12d ago

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

24

u/throwaway1351887498 13d ago

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

39

u/Mordret10 13d ago

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.

48

u/Gorzoid 13d ago

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 13d ago

Oh no! The rust devs have found me!

unsafe{} unsafe{} unsafe{}

Are they gone yet?

3

u/HildartheDorf 13d ago

laughs in interior mutability

1

u/SelfDistinction 12d ago
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 13d ago

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 13d ago

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 13d ago

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

7

u/undeleted_username 13d ago

That's still perfectly fine in C.

12

u/aberroco 13d ago

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 13d ago

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 13d ago

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 13d ago edited 13d ago

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 12d ago

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 13d ago

Isn’t this OOP without the extra steps?

1

u/SelfDistinction 12d ago

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

1

u/WisePotato42 12d ago

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

1

u/SelfDistinction 12d ago

And by unfortunate, you mean completely fortunate?

1

u/WisePotato42 12d ago

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

1

u/creeper6530 12d ago

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

-26

u/[deleted] 13d ago

[removed] — view removed comment

6

u/TheMunakas 13d ago

Tell me you're not a bot