when I recently checked a program written by a colleague, I found a function in it which is roughly as follows: void example (uint8 * pdata ){... if (null = * pdata) return; while (* pdata! = NULL ){...}...} at first glance, I felt that there was nothing written in the red part. When I saw while, I felt that the first sentence was redundant and there was no need to make such a judgment. When * pdata = NULL, while would not be able to get in, however, it was the first time to judge pdata, so it was estimated that my colleague wanted to write if (null = pdata) and written if (null = * pdata) by mistake ). so I told my colleagues that this is a bug, which may cause the program to read the NULL pointer and cause exceptions. After seeing it, my colleague said it was okay, that is, to judge * pdata = NULL. If it is to judge * pdata, there is no need to use the first if. It was first understood that null pointers cannot be operated, so pdata must be determined to be non-null. Because it was first found in the programmer's growth plan that the importance of non-null judgment on incoming pointer parameters is significant, and data writing to the NULL pointer will fail. Therefore, it is recommended that you do not perform this operation. My colleague said, how can I not read a null pointer? This problem is indeed difficult for me. If the NULL pointer allows reading, it will not be abnormal. (Now, this is also impossible. Although the program did not cause an exception, but this is to start reading the value at null, And the read value will also affect the operation, because this pointer, if it is a normal operation, it will not pass in null, which is more terrible than a direct exception, because if the bug is introduced elsewhere, it will increase the difficulty of identifying the bug. It is better to have a direct exception. This problem occurs when the structure is null.) for MCU, although there is no management of the operating system, however, null is generally the place where the starting address of the interrupt vector table is stored, especially the first jump command for power-on reset. Therefore, this segment of address does not make sense for the program to read, so it is also possible to use the C language to judge that the pointer is null. But will it cause exceptions? I'm not sure, but at least it will cause program running to be abnormal. A null pointer occupies a special address in C/C ++. Generally, it is used to judge the validity of a pointer. A null pointer is generally defined as 0 ,. The modern operating system retains a piece of memory starting from 0. The size of the memory depends on different operating systems. Once the program tries to access this memory, the system will trigger an exception/signal. Why does the operating system need to retain a piece of memory instead of just one byte of memory? This is because generally, memory management is implemented by Page Management, and at least one page must be retained instead of only one byte. Retaining a piece of memory also has additional benefits. You can check for memory errors such as injection p = NULL and P [1. In some embedded systems (such as ARM7, Cortex-M3), a piece of memory from scratch is used to protect the interrupt vector, without MMU protection, direct access to this memory does not seem to cause exceptions. However, this memory is code rather than the address of the valid variable in the program. Therefore, it is feasible to use a null pointer to determine the validity of the pointer. The secret to sound and fast writing in the programmer's growth plan. However, in Linux, the NULL pointer cannot be read, because the text end of the operating system starts from 0x08048000 ~ The space between 0x08048000 and MB is used to determine the content of the NULL pointer. In Linux, The IA-32 system starts from 0x08048000 (0x8000 in the arm9-kernel system ), there is a m gap between the start address and the lowest available address of the text segment, which is used to capture the NULL pointer. User space access is not allowed. Deep Linux kernel architecture NULL pointer definition pointers and integers are not interchangeable. zero is the sole exception: the constant zero may be assigned to a pointer, and a pointer may be compared with the constant zero. the symbolic constant null is often used in place of zero, as a mnemonic to indicate more clearly that this is a special value for a pointer. null is defined in . the C programming language Pa GE 92 still has some analysis not done. Is access to its members abnormal when the struct pointer variable is null? I once saw that it was determined by the compiler that the program compiled with GCC on my computer encountered exceptions during access. Other compilers did not know what would happen.
Null Pointer Analysis