What is the purpose of null?

后端 未结 25 2322
别那么骄傲
别那么骄傲 2020-12-07 22:24

I am in a compilers class and we are tasked with creating our own language, from scratch. Currently our dilemma is whether to include a \'null\' type or not. What purpose do

相关标签:
25条回答
  • 2020-12-07 23:27

    Null is a sentinel value. It's a value that cannot possibly be real data and instead provides meta-data about the variable in use.

    Null assigned to a pointer indicates that the pointer is uninitialized. This gives you the ability to detect misuse of uninitialized pointers by detecting dereferences of null valued pointers. If you instead leave the value of a pointer equal to whatever happened to be in memory then you would have crazily irregular program behavior that would be much more difficult to debug.

    Also, the null character in a C-style variable length string is used to mark the end of the string.

    The use of null in these ways, especially for pointer values, has become so popular that the metaphor has been imported into other systems, even when the "null" sentinel value is implemented entirely differently and has no relation to the number 0.

    0 讨论(0)
提交回复
热议问题