---------
How to Use the H and C files? In general, the H file is declare (Declaration), and the C file is define (Definition ). Because C files need to be compiled into library files (in Windows, yes. obj /. lib, which is in UNIX. o /. a) if someone else wants to use your function, you need to reference your h file. Therefore, the H file is generally a declaration of variables, macro definitions, enumeration, structures, and function interfaces, just like an interface description file. The C file is the implementation details.
The greatest use of an H file and a C file is the separation of declarations and implementations. This feature should be accepted, but I still see that some people like to write functions in H files. This is a bad habit. (For C ++, for its template function, only the implementation and description are written in a file in VC, Because VC does not support the export keyword ). In addition, if you write the function implementation in the H file, you have to add the dependency of the header file in makefile, which will make your makefile nonstandard.
Finally, the most important thing to note is: do not place the initialized global variables in the H file!
For example, there is a structure for handling error messages:
Char * errmsg [] = {
/* 0 */"No error ",
/* 1 */"Open file error ",
/* 2 */"Failed in sending/processing ing a message ",
/* 3 */"Bad arguments ",
/* 4 */"Memeroy is not enough ",
/* 5 */"Service is down; try later ",
/* 6 */"Unknow information ",
/* 7 */"A socket operation has failed ",
/* 8 */"Permission denied ",
/* 9 */"Bad configuration file format ",
/* 10 */"Communication time out ",
......
......
};
Please do not put this thing in the header file, because if your header file is subject to five function libraries (. lib or. a) used, so he is linked to these five. lib or. a, and if one of your programs uses the functions in these five function libraries, and these functions use this array of error information. Five Copies of the information are stored in your execution file. If your errmsg is large and you use more function libraries, your execution file will become large.
The correct statement should be written to the C file, and then the external Statement of extern char * errmsg []; should be added to the header of the C file that requires errmsg, let the compiler manage it only when linking. In this way, only one errmsg exists in the execution file, and this will facilitate encapsulation.
The most crazy thing I have ever encountered is that in my target file, this errmsg has a total of 112 copies, and the execution file is about 8 Mb. When I put errmsg in the C file and added the extern declaration to more than one thousand C files, the size of all function library files dropped by about 20%, my execution file is only 5 MB. 3 M less at once.
[Remarks]
-----
A friend told me that this is only a special case, because if errmsg has multiple copies in the execution file, it can speed up the program running, the reason is that multiple copies of errmsg reduce the system memory page feed and improve the efficiency. As we mentioned here, errmsg has only one copy. When a function uses errmsg, if the memory is too far apart, a page change will be generated, but the efficiency is not high.
This statement is not unreasonable, but in general, for a relatively large system, errmsg is relatively large, so the size of the execution file becomes larger due to the generation of copies, which not only increases the system loading time, it also allows a program to occupy more pages in the memory. For data such as errmsg, it is generally not frequently used during system operation, so it is not frequently generated for Memory Page feed. Under the balance, the efficiency of only one errmsg is high. Even for frequently-used data such as logmsg, the memory Scheduling Algorithm of the operating system will keep such frequently-used pages in the memory, so there will be no memory paging problems.