0xDEADBEEF vs. NULL

后端 未结 10 1624
醉梦人生
醉梦人生 2020-12-13 14:45

Throughout various code, I have seen memory allocation in debug builds with NULL...

memset(ptr,NULL,size);

Or with 0xDEA

10条回答
  •  盖世英雄少女心
    2020-12-13 15:03

    http://en.wikipedia.org/wiki/Hexspeak

    These "magic" numbers are are a debugging aid to identify bad pointers, uninitialized memory etc. You want a value that is unlikely to occur during normal execution and something that is visible when doing memory dumps or inspecting variables. Initializing to zero is less useful in this regard. I would guess that when you see people initialize to zero it is because they need to have that value at zero. A pointer with a value of 0xDEADBEEF could point to a valid memory location so it's a bad idea to use that as an alternative to NULL.

提交回复
热议问题