3 ms·
A pointer is a particular kind of reference (a value type that enables indirection), and C++ references are another (not very different) kind. A reference in C+
by evincarofautumn 15y ago
A pointer is a particular kind of reference (a value type that enables indirection), and C++ references are another (not very different) kind. A reference in C++ amounts to an immutable pointer.
Also, I didn't swap analogies: I traded analogy for metaphor. (A reference in a computer program is a conceptual reference in the real world.)
- esrauch 15y ago> A reference in C++ amounts to an immutable pointer. "Amounts to" is a phrase that lets you say pretty much anything. Eg: A pointer amounts to an int. If you are going to say that it amounts to an immutable pointer, you should at least add a caveat about how int *const x; would be different from int& x;
- evincarofautumn 15y ago"Nullability" would be that caveat. But that's an argument about C++, not an argument about references.
- TeMPOraL 15y ago> Eg: A pointer amounts to an int. Is there a reason why this analogy is invalid? I found it to be quite useful in using, understanding and explaining C pointers.
- cube13 15y agoThis is a really good example of an explanation that is correct in the basics, but almost completely incorrect and potentially dangerous when taken more in-depth. Pointers are not ints. First off, pointers are unsigned data types. Also, on 32-bit systems, they're 32-bit unsigned longs, and on 64-bit systems, they're 64-bit unsigned long longs. Both of those are incredibly important to understand, especially because the first can lead to very weird bugs with unsigned/signed comparisons or addition. I recently ran into a bug that involved that. 50% of the time, the program crashed with an incorrect memory address error. The other half, it worked completely fine. It turned out that we were casting a couple of pointers to signed values before doing some range checking. The cast converted them to hugely negative values, which broke the comparison check and sent us down a completely incorrect code path. The second is important when dealing with structure and class allocation sizes.
- esrauch 15y ago> Pointers are not ints. First off, pointers are unsigned data types. Also, on 32-bit systems, they're 32-bit unsigned longs, and on 64-bit systems, they're 64-bit unsigned long longs. Just want to point out that "int" is not a fixed defined size in C++ either, it just has a minimum size. You equally cannot depend on the size of int or void*.
- cube13 15y agoThis is true in regards to the C++ spec. An int data type is up to 32-bits. For old 16-bit architectures, an int and void* are both 16 bits. For more modern architectures and compilers(anything on a 32-bit processor or higher), ints are assumed to be 32-bit signed integers.
- kenjackson 15y agoA pointer is a particular kind of reference (a value type that enables indirection), and C++ references are another (not very different) kind. A reference in C++ amounts to an immutable pointer. I think you're beginning to walk that line where people start getting confused -- "I thought you said they were the same thing. Now you're saying they amount to some modified version of what you said?" Also, I didn't swap analogies: I traded analogy for metaphor. (A reference in a computer program is a conceptual reference in the real world.) I'm no grammar buff, but that seems to be walking a fine line. From my most recent CS SAT test: reference::computer as conceptual reference::real world In any case, many would argue that metaphor is equally dangerous. Djikstra has ranted on this very topic, saying it creates dangerous visualizations. My point is that there is way to teach the concept relying on neither analogy nor metaphor, but a more basic concept of how data is stored. I think you'll find different students will react favorably to different techniques.
- cube13 15y ago>A pointer is a particular kind of reference (a value type that enables indirection), and C++ references are another (not very different) kind. A reference in C++ amounts to an immutable pointer. A pointer is a memory address. That's all it is. It can contain any valid memory address for the system. A C++ reference is essentially a hard link to another declared object. As far as the compiler is concerned, a reference to an object is the object, and therefore has all the attributes that the original object has. Pointers are like a house street addresses. They do not directly allow the user to see anything about the house, but they tell the user where the house is. References are like what people call a certain house. I may call the house I live in "home", but my friend Bob may call it "cube13's house". Both myself and Bob use different terms for it, but we both mean the exact same physical location.