Summary of common causes of Linux segmentation faults

During Linux development, segmentation faults may often be encountered during the debugging phase, that is, segment faults appear in the log. The following summarizes several causes of segmentation faults, which is convenient and targeted to quickly find and locate problems.

Most segfaults are caused by memory usage errors:

1 Use a large data structure as a function's local variable, which may cause stack errors when the function call is nested in many levels

2 Memory data copy, pointer use error, such as adding address symbol without adding, and not adding when it needs to be added

3 The size of the two buffers of memcpy is inconsistent

4 malloc allocation and release do not match

5 The element value of the pointer is included in the print, but the pointer is empty, then printing will cause a segmentation fault

6 To change the value of the content pointed to by the pointer, use the pointer parameter. If you want to change the content pointed to by the pointer, that is, to change the direction of the pointer, you must use the pointer of the pointer as a parameter. If you use it incorrectly, it will cause a segmentation fault.

7 Same as above, if the parameter is a pointer, the address is forgotten when passing the parameter, or vice versa

8 There are uninitialized local variables. Generally, the compiler initializes global or static variables to 0, while local variables must be initialized manually. If no initialization is done, the initial value of the variable may be a random value. Using random values ​​for memory operations can easily lead to segmentation faults, and the segmentation faults caused by this situation will not appear every time you run, which will increase the difficulty of problem location

9 When the driver needs to load the firmware and initialize the data, it encounters a segfault when it runs. The reason is that new data is added under flash data, and it is not erased during programming, thus importing dirty data and causing a segfault.

Guess you like

Origin blog.csdn.net/wwwyue1985/article/details/112426451