4 ms·
> Of course C has references because C has pointers. References in C++ are just constant pointers. This is incorrect. You cannot have a reference to nullptr,
by sausagefeet 15y ago
> Of course C has references because C has pointers. References in C++ are just constant pointers.
This is incorrect. You cannot have a reference to nullptr, for example. Pointers and references are different beasts, nowhere in the C standard does it refer to pointers as references. The underlying representation in compilers does not imply equivalence.
- Roboprog 15y agoAbsolutely. Having learned Pascal before C, I really missed pass-by-reference for quite some time. Efficiency wise, a reference is just a hidden pointer, BUT, it is nice to know that the reference CANNOT be null. The caller of a routine expecting a reference must have actual data to pass, or the routine never gets called. C is a very handy portable assembler, though.
- z92 15y agoYou might like to read "Moron Why C is NOT Assembly" http://james-iry.blogspot.com/2010/09/moron-why-c-is-not-assembly.html http://james-iry.blogspot.com/2010/09/moron-why-c-is-not-ass...
- tbrownaw 15y agoI've seen code that does things like "Foo &foo = * ((Foo * )0);" (possibly split among multiple statements). It seems to work fine, I suppose it's really undefined behavior?
- thisuser 15y agoDereferencing a pointer to 0 seemed to work fine?
- tbrownaw 15y agoI think what happened was that the reference was passed to a function that (1) under most conditions (I think there was a fast-path added after the function had been around a while) accessed it directly, and (2) under all conditions turned it back into a pointer for another call. The way that function was called in this particular case was outside of those "most conditions", so the only thing that was done with the invalid reference was to turn it back into a pointer and then null-check it. And so while making the reference probably counts as "dereferencing" the pointer as far as language rules go, the memory that it pointed to was never actually accessed. (Why yes, that does sound like something badly in need of refactoring. And illegal reliance on implementation details.)
- apaprocki 15y agoOn AIX, page 0 is mapped and readable, so dereferencing null works just fine. :)
- caf 15y agoActually, the C standard does say: A pointer type describes an object whose value provides a reference to an entity...
- sausagefeet 15y agoReferences are a C++ creation with a precise definition. This is what this quote is talking about, considering C does not have references.