A thought caused by negligence! (Strerror), neglected strerror
A few days ago, the code was written due to your negligence, resulting in the Segmentation fault (core dumped) over and over again ). This problem occurs because the pointer returned by strerror (errno) points to an invalid memory, leading to program printing errors and crashes.
Failed Attempts several times, for progress. Replace perror (str) with a simple and crude one ). I was busy today and found my problem recorded and shared.
All pitfalls are self-built. Start.
1 #include<stdio.h> 2 #include<errno.h> 3 //#include<string.h> 4 int main() 5 { 6 char *perr = NULL; 7 errno = 14; 8 perr = strerror(errno); 9 puts(perr); 10 return 0; 11 }
Check the code first. I have blocked # include <string. h>. As I expected, the compilation passes (in fact, warn has already told you that there is a problem) and the running crashes. Warn is as follows:
Then, the # include <string. h> program is perfectly compiled and executed successfully. Why? I have discussed and browsed the post on stackoverflow with someone (CSDN), and finally used gcc-E to pre-compile gcc-Wall for verification.
I have the following conclusions.
1. The strerror () function is declared in the string. h header file (I used to think it was in errno. h, or the basic skills are not good ).
2. If an undefined function is found during gcc compilation, it will think that the function is defined in other source files, so the compilation is successful.
However, because the compiler does not see the function prototype, it considers the return value of the function as int. In the link stage, if the function is found, the system returns an error.
Here I have done an experiment. If the function prototype is no problem after it is linked in the. o file, the connector will correct the return value type. However, if the file is linked to the. so file, the returned value is int. I cannot understand.
Reveal the answer! Why does my program report an error? This is the consequence of a series of negligence and basic skills.
Because I do not have the string. h package. Therefore, the compiler cannot see the function prototype. The default function return value is int. It also provides wara: assignment makes pointer from integer without a cast (assign a value to the pointer using an int type without conversion-poor English ). Obviously, in a 64-bit system, char * is 64-bit, and int Is 32-bit. So I assigned a pointer with a 32-bit integer value as the address. So the Pointer Points to invalid memory. Then Segmentation fault (core dumped) will be launched.
In the system, functions that return pointers such as strerror () may have similar problems. Therefore, the header file must be included in the package. Note that the warn cannot be ignored.
This article has ended. Give an appendix. 64-bit gcc compiler for each data type length table.
The above description is written by a non-authoritative person. If you are familiar with it, please follow the instructions in depth.