NULL function pointers

后端 未结 3 1896
野性不改
野性不改 2021-01-17 11:04

What is the behavior of calling a null function pointer?

void (*pFunc)(void) = NULL;  
pFunc();

Why is it advisable to initialize yet unu

相关标签:
3条回答
  • 2021-01-17 11:24

    It's advisable for the same reason as initializating "normal" (data) pointers to NULL: because it potentially makes some errors easier to track down. Opinions on whether this is useful or not of course vary :-)

    0 讨论(0)
  • 2021-01-17 11:30
    1. What happnes when u try to access NULL? Following is true about data as well as code, and this is what happens when you try to read NULL(or any address from 0 to 4096,i.e atleast first page of segment). Root cause of this lies in OS and microprocessor segmentation/paging architecture

      When you try to access NULL( or 0) address, in any of data or code section, it causes segmentation fault(which is actually a killer page fault). First page of section is treated as out of( or invalid part of) virtual address space. That is purposefully that first page is kept invalid( or not present) so atleast one address that pointer contains could be represented as invalid in program at execution time.

      Page descriptor of the 1st page(which contains virtual address 0, NULL), has first bit "present" as 0 (means its invalid page). Now if you try to access NULL pointer(0 address) it will cause to raise a page fault as page is not present, and OS will try to handle this page fault. When page fault handler see that its trying to access 1st page, which is treated as a invalid part of virtual address space it kills the process. This is all about user space process. If you try to access NULL pointer in system process(kernel level code), it will fail your OS an crash the system.

      Links: http://en.wikipedia.org/wiki/Page_fault#Invalid http://en.wikipedia.org/wiki/Memory_protection#Paged_virtual_memory http://pdos.csail.mit.edu/6.828/2005/readings/i386/s05_02.htm

      Above is sufficient bt as i think u should read this as well http://www.iecc.com/linker/linker04.txt

    2. Why function pointer is initialized to NULL? Although if you try to call the with NULL its going to give page/segment fault. NULL signifies its invalid function. If it contains any garbage address but in valid virtual address space of code section, i think any code at that address will be called, which could be even more disaster(spl in case of real time systems). Initialize funcp = funct_foo_name + 1; now call function using function pointer. Function pointer points to valid virtual address space of code section. bt function will start from incorrect place to execute. which could result into wrong code execution or wrong order.

    0 讨论(0)
  • 2021-01-17 11:38

    In C and C++, this is called undefined behaviour, meaning that this can lead to a Segmentation fault, nothing or whatever such a case will cause based on your compiler, the operating system you're running this code on, the environment (etc...) means.

    Initializing a pointer to a function, or a pointer in general to NULL helps some developers to make sure their pointer is uninitialized and not equal to a random value, thereby preventing them of dereferencing it by accident.

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