AIX and Linux C language compiler difference summary

AIX and Linux C language compiler difference summary

xlc++ and g++

The xlc++ compiler is used on AIX, and the g++ compiler is used on Linux.
For behaviors that are not strictly defined in the C standard, the processing methods of the two compilers are not necessarily the same, and the code will behave differently when running on the two platforms. As a result, it runs normally on one platform, but the other platform may be a bug.
The problems are concentrated in the following areas

Dangerous code example xlc++ compiler g++ compiler Approach
s=”12”<br>sprintf( r,"%04s",s ); s is less than four digits left to complement '0' <br> r=”0012” s is less than four left-filled blank characters''<br>r=” 12” Add check code<br>char p = r;<br>for (; p == ''; ++p)<br>*p='0';
When file descriptor file=NULL, <br>write content fprintf(file, "..."); Unable to write, the program continues to execute downward program crash Add judgment<br>if (file != NULL)
memcpy copies array data out of bounds The addresses among variables are scattered, and the array itself is out of bounds, which has no effect on other variables, and normal results can generally be obtained. The addresses of the variables are closely adjacent, and the array itself is out of bounds, and the value of the adjacently defined variable will also be overwritten, resulting in abnormal results. Check the length of the copied memory at the corresponding location to avoid crossing the boundary
Structure s contains double variable a, which is not initialized before use a default value 0.0 a The default value is a very large positive number, causing the program to crash Increase initial processing memset(&s,'\0', sizeof(s));

32-bit system and 64-bit system

If one of the two platforms is a 32-bit version and the other is a 64-bit version, the length of some variables will change. The places that need attention are as follows:

Variable type 32-bit system 64-bit system Dangerous code and treatment
long 4 bytes 8 bytes Avoid using sizeof(long) to get the length value
pointer 4 bytes 8 bytes The third parameter of some memcpy and strncpy in the code is sizeof (pointer). After migration, the length of the copied memory changes (4 becomes 8) and the result is abnormal. It needs to be modified according to the situation to avoid using sizeof to manipulate variables whose length varies with the platform. When the array name is used as a parameter, it is also equivalent to a pointer, and the sizeof operation should not be performed.

Guess you like

Origin blog.51cto.com/14947900/2539914