Dereferencing a pointer to 0 in C

前端 未结 5 1332
抹茶落季
抹茶落季 2020-12-08 06:33

Sometimes data at memory address 0x0 is quite valuable -- take x86 real mode IVT as a more known example: it starts at 0x0 and contains pointers to interrupt handlers: a dwo

5条回答
  •  渐次进展
    2020-12-08 06:40

    The operating system use a table of pointers to interrupt routines to call appropriate interrupt(s). Generally (in most operating system) table of pointers is stored in low memory (the first few hundred or so locations), These locations hold the addresses of the interrupt service routines for the various devices.

    So when you do

    char *ptr = 0x0; 
    

    then most likely you are initializing your pointer with the address of an interrupt service routine. Dereferencing (or modifying) a memory location which belongs to operating system most likely cause program to crash.
    So, better not to initialize a pointer to 0x0 and dereference it until you have the confirmation that it doesn't belongs to OS.

提交回复
热议问题