r/C_Programming • u/gogsboonerisms • Apr 24 '24
The way C Programers explain pointers
https://imgs.xkcd.com/comics/pointers.png14
u/Jonny0Than Apr 24 '24
The funniest thing about this comic is that 64-bit CPUs were just becoming commonplace (for consumers) around the time it was posted.
(The addresses given are all 32-bit)
11
u/legends2k Apr 24 '24
Well, as an artist Randall might have found putting 64-bit addresses more noisy visually and 32-bit addresses drives home the point just fine.
6
u/FlyByPC Apr 24 '24
And an artist technically savvy enough to have created Little Bobby Tables probably knows about both.
30
u/EpochVanquisher Apr 24 '24
There are pointers in most languages.
The difference is that C calls them “pointers”, makes you manage lifetime manually, and lets you do arithmetic on pointers. But the hard part about learning to use pointers is not any of the things unique to C; instead, it’s understanding conceptually how object references work; it’s understanding that two different references can refer to the same object.
It’s one of the most common gotchas that trips up beginners in Python, for example.
17
u/Marxomania32 Apr 24 '24
Being able to do arithmetic on pointers and be able to arbitrarily dereference them is what makes them pointers. Other languages have references, which almost certainly end up being implemented with a pointer somewhere underneath the hood, but that doesn't make them pointers.
0
u/EpochVanquisher Apr 24 '24
Incorrect. People still call them “pointers” even if you can’t dereference them at all (like
void*). People also still call them pointers even if you can’t do arithmetic on them (like function pointers).13
u/Marxomania32 Apr 24 '24
That's a good point, but I still disagree since, in the end, those pointer types can always be cast to at least one different pointer type (
char *) that you can do pointer arithmetic and do arbitrary dereferences on. Reference semantics ultimately do have entirely different semantics from pointers, so they are different.0
u/EpochVanquisher Apr 24 '24
“Reference semantics” and “pointer semantics” are kind of vague terms. The terms are used differently in different languages. C has pointers in the language standard but “references” is not a technical term in C. C++ has a technical notion of references but it is different from the semantics for references in other languages.
0
u/glasket_ Apr 24 '24 edited Apr 24 '24
tl;dr within the type hierarchy, pointers and reference types are subtypes of abstract references, making them distinct types but equivalent as references.
Function pointers are a unique kind of
referencepointer type that can't be substituted with object pointers in either direction, so they're an entirely distinct type. added: The problem is that C defines pointer in such a way that it's just a distinct object which references another object or function, which is pretty vague, so the actual type semantics depend on the derivation. In essence, C doesn't have a pointer type, but it has object pointer types and function pointer types, which are loosely grouped as pointer types under the standard, with object pointers often simply being called pointers.
void*, meanwhile, is just a supertype of object pointers with restrictions implied by the language semantics (arithmetic and access can't be reasoned about without a size). It's effectively an "abstract object pointer" in how it's defined by the standard.edit: I suppose the best way to actually summarize the above is that C has "object pointers" and "function pointers" as distinct concrete pointer types, with the only notion of an abstract pointer overall being "it's an object which refers to something". Added and
removedwords in the above to make this more clear, and removed sentences entirely that clouded this. /editThis directly follows through to your original Python example. The reference types in Python and similar languages aren't pointers. There's nothing about the types themselves that makes them pointers; they behave like pointers because both reference types and pointers are subtypes of abstract references, but the concrete types in use in C and Python are distinct from one another. This shines through most in C#, where both pointers and reference types are available, which makes the type distinction obvious.
2
u/EpochVanquisher Apr 24 '24
Sure. But they are still all called “pointers” in C.
“Pointer” is just a name people use. It doesn’t have a rigorous definition which everyone agrees on. It just has various contextual uses that share a lot of similarities—like, when a C programmer talks about pointers, that includes function pointers and void pointers. When a C# programmer talks about pointers, they’re talking about things like “unmanaged pointers” which is a meaningless term in most languages.
1
u/glasket_ Apr 24 '24
I get where you're coming from, but I think that's why using reference instead of pointer makes more sense as the generic term. An abstract reference is more well-defined by theory to be anything which references something else, whereas pointer can be a bit wishy-washy with expectations.
I can see how someone could take "a pointer points at something" and interpret it as being 1:1 with an abstract reference, but other people associate it with a distinct value that can be interacted with or a form of direct reference.
I'll admit I haven't thought about people interpreting pointer this abstractly before, but I do feel reference better serves the purpose when used in this way to avoid confusion.
2
u/EpochVanquisher Apr 24 '24
This is kind of an issue with pedagogy, where you try to find the best word for teaching students a concept that has the least probability of confusion and the maximum probability of understanding.
I'll admit I haven't thought about people interpreting pointer this abstractly before, but I do feel reference better serves the purpose when used in this way to avoid confusion.
There’s a pedagogical purpose to saying “Python reference types are pointers” and that’s to get people to think about the ways in which that statement is true. The purpose is not to tell people something like “Python reference types and C pointers are exactly the same”. I hope people are smart enough to understand that they’re not exactly the same, but that there is a lot that they have in common.
In particular, a Python object in CPython has a memory location, a variable which refers to an object contains that object’s memory address. If you know how pointers work, then you know something about the way Python works. Even if it doesn’t suddenly unlock a bunch of new understanding, it helps you make connections between the programming practices you have in C and the ones you have in Python, and it helps you ask interesting questions about Python semantics (like why
+=modifies a list in-place, but doesn’t modify an integer in-place).The catch here with terminology is that terminology only gets you so far—you can have a long discussion about whether “pointer” or “reference” is the right word for something, but once you get to a certain level of detail, you need to discuss things in more concrete, specific terms.
-5
u/lokithegregorian Apr 24 '24
"Pointer" is a lexeme of all programming languages. Like "instantiate," "reference," "object," or "value." It is the object holding your reference (usually to memory). It has a size. I can set other variables equal to it. I can pass it in functions. I can operate on the pointer, with or without de-referencing said pointer.
It doesn't matter what the language calls them. They are simply rebranding a pointer.
https://en.wikipedia.org/wiki/Pointer_(computer_programming)
7
u/erikkonstas Apr 24 '24
This is incorrect; for example, Python doesn't have pointers at all.
0
u/lokithegregorian Apr 24 '24
You are a self taught coder.
The python interpreter uses pointers all the time. A list or an array in python? That's just pointers to a list of pointers. Python saves you that trouble. It also denies you the power that comes with controlling the granularity. C, does not. C gives you live ammo, and trusts you to not shoot yourself.
It's not even that it trusts you. It trusts you no more than a rock and a high cliff trusts you. The makers never cared what you would do with it. You can shoot yourself all day. You want a safety? Make one with C and make a library that you reference. You'll want to use "pointers" in that solution; they make for cheap data integrity.
2
u/erikkonstas Apr 24 '24
Yes, I do agree that the CPython implementation uses pointers. The language itself still has no pointers. Java is another language with no pointers.
1
u/lokithegregorian Apr 24 '24
What does the python interpreter use to pass objects that hold memory references? And that name, it may be a label, that references a computer science term. What term is that?
2
u/erikkonstas Apr 24 '24
Exactly, the interpreter... not the language itself!
1
u/lokithegregorian Apr 24 '24
Python is an interpreted language. Because of that, the language does not exist without the compiler.
...unlike C. xD
3
u/erikkonstas Apr 24 '24
The language itself is not the same as its implementation; rather, it is defined by its specification. Otherwise, C wouldn't "exist" without a compiler either!
→ More replies (0)2
1
u/glasket_ Apr 24 '24
You should read the Reference page instead. Pointers are only one kind of reference, and the underlying implementation of a language has no impact on what its type system actually conveys.
If you aren't directly interacting with a pointer type, you aren't using a pointer; you're using a reference which may or may not act as an abstraction over a pointer within the implementation.
1
u/lokithegregorian Apr 24 '24
A reference would be the literal. If I set a variable equal to that literal, what is that variable now called?
2
u/erikkonstas Apr 24 '24
Weirdly enough, I have understood pointers for longer than I've understood references 😂 it's almost like mastering pointers is kinda necessary to understand references, and even then I still needed some practice with those... before that I just knew about Python's "quirky" way (Markdown messed up here)
a = b = []works without considering the deep details of it...0
u/EpochVanquisher Apr 24 '24
Yeah—the thing is,
a = b = []works the same way in Python as it does in, like, most languages, with a couple exceptions like C++. Like, Java, C#, JavaScript, Go, and Ruby all work the same way as Python does. C++ is the weird one for copying lists by default, if you are using something likestd::vector.1
u/erikkonstas Apr 24 '24
Yeah it took some time to realize that hey, it's just a reference, even though there's no reference to "reference" here (aside some CPython-specific stuff)!
isand theidbuilt-in also make much more sense now...1
u/Jarhyn Apr 24 '24
Everyone, when starting to learn pointers, should do the bucket exercise.
The bucket exercise is when you and a couple buddies have a bunch of numbered buckets.
The buckets are the "physical addresses", and their contents are the "data at address". Buckets can contain only values that represent other buckets.
Then you can take turns asking for the "literal value in the bucket", the "indirected" value in the bucket, and "the address of the bucket".
After this exercise you should hopefully be able to concretely distinguish between the idea of "the contents of the bucket", "the identity of the bucket", "the identity the contents of the bucket's reference", and "the contents of the bucket referred to by the bucket's reference" and so on.
0
u/Jonny0Than Apr 24 '24
Strong disagree on "the hard part." Dangling pointers, double-free, memory leaks, and buffer overruns are much harder to prevent and debug, and they're all (mostly) unique to C/C++. And that's *on top of* the concept of references.
4
u/EpochVanquisher Apr 24 '24
My experience from teaching—once people have a good grasp of pointers, that’s the hard part, and the other stuff built on top of it is a lot easier. When people have a hard time with double-free, it’s often because they don’t understand that an object and the reference to the object are separate objects, and changing one doesn’t affect the other.
-2
3
3
u/oh5nxo Apr 24 '24
Funny to make the bytes all ASCII and not contain a message.
4
Apr 24 '24
The message reads:
:(!:c99,sch.if the characters are read in sequence. At least the
c99bit seems relevant.1
u/oh5nxo Apr 24 '24
Either there's a joke, or there not being any joke IS the joke. Voynich in 12 characters.
:!(:,99c.hcs*scratch*
2
Apr 24 '24
In reality, those are the pointers to where the solutions to his problems are currently stored in the game memory.
1
Apr 24 '24
This meme is way funnier if you imagine it as a genuine conversation between a wholesome son and his suddenly NPC, AI chatbot dad
1
1
1
u/StochasticTinkr Apr 24 '24
The real joke is that those are where the health, stamina, and damage are stored. Just use CheatEngine to update them and the game is easier.
1
u/Wehraboo2073 Apr 24 '24
with these pointers he can now lock his hp and change his gold to 9999999999
22
u/gremolata Apr 24 '24
You can tell the joke is old because these are 32-bit ones!