News for hackers. Stuff that matters.
Re:How can C Programs be so Reliable? by tomp on Thursday September 22, 2011 @07:54AM (#3025731)
> like passing around a reference to a struct

I thought that references are a feature of C++, not C. Personally, I never really got references... They are just a kind of magical pointers that programmers can forget about, but they make the code much less readable and can interact in funny ways...

Re:How can C Programs be so Reliable? by bad_user on Thursday September 22, 2011 @08:07AM (#3025750)
Btw, I corrected my statement -- I was referring to "a struct's member" from which later you can retrieve the actual struct that owns that value.

Of course C has references because C has pointers. References in C++ are just constant pointers.

Re:How can C Programs be so Reliable? by sausagefeet on Thursday September 22, 2011 @11:23AM (#3026358)
> 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.

Re:How can C Programs be so Reliable? by Roboprog on Thursday September 22, 2011 @12:27PM (#3026614)
Absolutely. 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.

Re:How can C Programs be so Reliable? by z92 on Thursday September 22, 2011 @01:15PM (#3026805)
You might like to read "Moron Why C is NOT Assembly"

http://james-iry.blogspot.com/2010/09/moron-why-c-is-not-ass...

Re:How can C Programs be so Reliable? by tbrownaw on Thursday September 22, 2011 @03:31PM (#3027428)
I'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?
Re:How can C Programs be so Reliable? by thisuser on Thursday September 22, 2011 @03:56PM (#3027522)
Dereferencing a pointer to 0 seemed to work fine?
Re:How can C Programs be so Reliable? by tbrownaw on Thursday September 22, 2011 @04:33PM (#3027673)
I 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.)

Re:How can C Programs be so Reliable? by apaprocki on Thursday September 22, 2011 @10:25PM (#3028600)
On AIX, page 0 is mapped and readable, so dereferencing null works just fine. :)
Re:How can C Programs be so Reliable? by caf on Thursday September 22, 2011 @08:48PM (#3028389)
Actually, the C standard does say:

  A pointer type describes an object whose value
  provides a reference to an entity...
Re:How can C Programs be so Reliable? by sausagefeet on Tuesday September 27, 2011 @02:31PM (#3044602)
References are a C++ creation with a precise definition. This is what this quote is talking about, considering C does not have references.