this post was submitted on 13 Jul 2023
668 points (96.6% liked)

Programmer Humor

31250 readers
2655 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 4 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] abraham_linksys@sh.itjust.works 5 points 11 months ago (1 children)

So it's sort of like "proxying" through pointers to enforce memory isolation?

[–] AlmightySnoo@lemmy.world 10 points 11 months ago* (last edited 11 months ago) (1 children)

I'm not entirely sure what you mean by memory isolation here, but the basic idea is that if you have a pointer to something then you know where it is located in memory and you can write in it, that's the whole idea of passing by address in C.

Now pointers themselves are merely variables. Yes they have a special meaning, they "point" to something and you can dereference them with the * operator, but at the end of the day they're still variables. They have a physical existence in memory or CPU registers, and their content is simply the address to which you want to point. Once you accept this, then the idea of the address of a pointer (ie the location of the variable you're calling "pointer", and not the address it contains) is not strange anymore and you can perfectly have a pointer-to-pointer in order to, among other things, pass pointers by address.

[–] Iridium@lemmy.world 7 points 11 months ago (2 children)

that’s the whole idea of passing by address in C

Wait stop, so in other languages like C#, when you pass a variable into a function “by reference” is that just passing the pointer to the variable?

Have I been baited into using pointers my whole life?

[–] olorin99@kbin.social 9 points 11 months ago

Yes passing "by reference" is essentially the same as "by pointer" but with some syntactical sugar to make it easier to work with.

[–] pazukaza@lemmy.ml 6 points 11 months ago* (last edited 11 months ago)

In C# it is different.

In C if I give you a pointer to a memory address, you can totally overwrite what is in that memory address, even write a new struct in there. So you're getting a "real" writable memory address. You could even write a different type of structs and break the program. You could tweak specific bytes.

In languages like Java or C# you aren't given a reference to the memory address but a reference to the object. You can only write to the object using it's own interface (methods) but you can't say "I'm going to totally overwrite this memory address with a new object".

If you receive an object in a parameter, let's say a "Person person" object and you do something like "person = new Person();" you didn't really overwrite the memory address. The original person reference that was passed in the parameter is still intact. You can only modify it with something like "person.setName(...)".

So, with real pointers you can do more stuff, but higher level languages don't want you to do that because it breaks some of their principles for what "good programming" is. In this case they are protecting encapsulation. You shouldn't be able to mess around with the memory contents of objects directly, only through their interfaces. Encapsulation is safer because objects should expose how to operate them safely via their interfaces.