Hacker News new | past | comments | ask | show | jobs | submit login

I modified the program so that the main function looks like:

  say = NULL;
  say("OH NOES");
When compiled with all warnings on, there are none. When run, it immediately segfaults. (The same would happen with any assignment from void*, like 'say = malloc(42)'. Nice.)

Edit: I knew this would get downmodded :) Why does any post implying that type safety is good get downmodded?




Maybe because people know by now that C doesn't hold your hand :). The compiler did exactly what you told it to do. And, in fact, since C is frequently used for writing operating system code, what you wrote may actually be a valid operation if the OS maps critical functions into page 0 (which I believe some older systems did).


what you wrote may actually be a valid operation if the OS maps critical functions into page 0

Then you'd better not call it NULL. "0 and NULL are the same" was how I first got root on my phone ;)


In C++ 0 and NULL are the same by definition. Just look at Stroustrup's C++ Style and Technique FAQ.


Assigning the value 0 or NULL to a pointer does not necessarily set it to address 0.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: