Why NULL is not predefined by the compiler

前端 未结 4 1682
广开言路
广开言路 2021-01-13 06:33

This issue bothered me for a while. I never saw a different definition of NULL, it\'s always

#define NULL  ((void *) 0)

is there any archi

4条回答
  •  礼貌的吻别
    2021-01-13 06:57

    In the dark ages before ANSI-C the old K&R C had many different implementations on hardware that would be considered bizarre today. This was before the days of VM when machines were very "real". Addresses of zero were not only just fine on these machines, an address of zero could be popular... I think it was CDC that sometimes stored the system constant of zero at zero (and did strange things happen if this was set non-zero).

     if ( NULL != ptr )     /* like this */
     if ( ptr )             /* never like this */

    The trick was finding address you could safely use to indicate "nothing" as storing things at the end of memory was also popular, which ruled out 0xFFFF on some architectures. And these architectures tended to use word addresses rather than byte addresses.

提交回复
热议问题