4 ms·
> 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
by 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.