What would we do without NULL?

后端 未结 11 908
迷失自我
迷失自我 2021-02-01 05:26

I once read that having nullable types is an absolute evil. I believe it was in an article written by the very person who created them(in Ada?) I believe this is the article

11条回答
  •  囚心锁ツ
    2021-02-01 05:58

    Realistically speaking, in any powerful programming language that allows pointers or object references in the first place, there are going to be situations where code will be able to access pointers which have not had any initialization code run upon them. It may be possible to guarantee that such pointers will be initialized to some static value, but that doesn't seem terribly useful. If a machine has a general means of trapping accesses to uninitialized variables (be they pointers or something else), that's better than special-casing null pointers, but otherwise the biggest null-related mistakes I see occur in implementations that allow arithmetic with null pointers. Adding 5 to a (char*)0 shouldn't yield a character pointer to address 5; it should trigger an error (if it's appropriate to create pointers to absolute addresses, there should be some other means of doing it).

提交回复
热议问题