Problems

xiaoxiao2021-03-06  50

Basic explanation

This section mainly explores a range of common programming issues caused by the characteristics of the two aspects of the C compiler. Compiling C files: C program is usually composed of several small programs (.c files), the compiler compiles these small programs, and then combines them together to form a target code together via the linker. Since the compiler can only compile a file each time, it cannot immediately check the error that requires several source files to find. Establishing a temporary variable C compiler to establish a temporary variable C compiler for the function of the function, which can also pass a pointer to the function's return value. Because these temporary variables have the existence of implies, in some cases, in particular, it will lead a series of problems when they exist. The header file contained in the C file and the C language together with the header file included in the C language is compiled with the .C file, the problem in the header file is reflected in the compilation of the .C file. Question: Compiling the C file I have an array A defined in F1.c, but I want to calculate the number of elements in F2.c, can I achieve this with SIZEOF? Answers and analysis: The answer is negative, you have no way to achieve the purpose, essential reason is that the SIZEOF operator works just "Compile Time", and the C language compiler is compiled each time. C file ( Other languages ​​are also the case). Therefore, SIZEOF can determine the size of an array in the same source file, but it is incomplete to the array defined in another source file, because it is already "run time" to determine what is determined. One thing to do, there will always be a way, and there are three optional methods to solve this problem: 1), define a global variable, let it remember the size of the array, in another .C file we Get the size information of the array by accessing this global variable (it seems to be a bit small question. 2), in a .h file, macro define the size of the array, such as #define array_size 50, and then contain this .h file in the two source files, get the definition in different .c files by directly accessing array_size. The size of the array. 3), set the last element of the array for special values, such as 0, -1, null, etc., then we look for this special end element by traversing an array, thus judge the length of the array (which is less efficient, but also stupid ). Question: The function return value implies the code below the pointer to work normally, but there is a fatal error generation at the end of the program. what is the root cause?

Struct List {char * item; struct list * next;} main (argc, argv) {...} answer and analysis: The reason is very simple, a little attention is not difficult to find, add one after the defined structure List The semicolon can solve this problem:

Struct list {char * item; struct list * next;}; // missing this semicolid! Ok, the problem is solved, but do you know what a fatal problem is this error? The problem is not as simple as the surface, OK, let us take a look at the truth behind things. First take a look at the code below:

Void func (struct my_struct stx) {.......} struct my_struct stand = {...}; func (style); when the function func is called, copy the value of the structural variable STY to the call. In the stack, it is passed to the function func as a parameter, and the parameter value called C language is passed. I believe this must be very clear, then you should know: How should the function return to the call if the return value of the function is structural variables? And look at the code below: struct my_structfunc (void) {.......} struct my_struct stand = func (); at this time, the return value of the function FUNC is a value of a structural type. This return value is placed in memory. One of the dark and horrible places, and arranged a pointer to this place (temporarily called "mystery pointer"), and this pointer will be passed to the function func as a hidden parameter as a hidden parameter. When the function func returns, the code generated by the compiler copies the value of the memory area of ​​the hidden pointer to the return structure STY to complete the return structure variable value to the caller. You understand the above-mentioned Dongdong, then today's true reasons will come out: because there is no differential number after the definition of struct list {...}, resulting in main function main (argc, argv) is compiled by compiler In order to return the value of the value of the structural variable, it is desirable to get the third parameter other than Argc and Argv, that is, the implicit "mysterious pointer" mentioned above. However, everyone knows that the function is the main function, and the parameters of the main function are provided by the startup code in the program (STARTUP CODE). The startup code, of course, main () is born to get two parameters, to "mysterious pointer", of course, this () main () is returning to the self-propelled self-emptive to visit its access to it does not exist The third parameter (ie, mystery pointer), which leads to illegal access to produce fatal problems. This is the true root of this problem. Recommendation: 1), try to use the pointer of the structural variable instead of the structure itself as a function parameter, otherwise the overhead of the memory copy is not small, especially for those who are frequent, the structure is large. 2) After the structure definition must add a differential number, after the above paragraph tells, I believe you will not make the same mistake. Question: The compiler will impose a temporary copy of the parameters of the function. What is the result of the Test function below?

Void getMemory2 (char ** p, int num) {* p = (char *) malloc (num); void test (void) {char * str = null; getMemory (& STR, 100); strcpy (Str, "Hello "); Printf (str);} answer and analysis: This is the example above Lin Rui's" C / C High Quality Programming Guide ", which is used. This call will generate the following two consequences: 1), can output Hello 2), the memory leaks another related issues: What kind of results will there be a Test function?

Void getMemory (char *) {p = (char *) malloc (100);} void test (void) {char * str = null; getMemory (STR); strcpy (str, "hello world"); Printf (STR) } Answer and analysis: The consequences are serious, the result is the result of the program crash, and by running the debug. We can see that after getMemory, the STR in the Test function is still NULL. It is conceivable that a call is StrcPy (STR, "Hello World"); the program inevitably collapse. Cause Analysis: The C compiler will always make temporary copies for each parameter of the function, and the copy of the pointer parameter P is _P, the compiler makes _p = P. If the program in the function is modified, the contents of the parameter P are modified accordingly. This is why the pointer can be used as an output parameter. In this example, _P applies for new memory, just changing the memory address referred to in _P, but p is unchanged. So the function getMemory does not output anything. If you want to output dynamic memory, use the pointer to the pointer, or use the pointer to the reference. Question: The header file and the .C file containing it compiles the code below very short, it seems unpleassed, but the compiler reports an error. Where can I appear?

#include "someheader.h" int myint = 0; answer and analysis: No need to stare at int myint = 0; see, this sentence assignment should be the simplest statement in the C language, the problem will definitely not be in it, then a problem Only in SomeHeader.h, the most common is the declaration of the last line of the header file (function, variables) does not use semicolons ";", then the compiler will combine it with myint variables. Considering that it will naturally be wrong. This problem is mainly reminding you that ideas should be wanncted when positioning problems, and may consider whether there is a problem with the header files. Conclusion: The header of the included header is compiled with the .C file, the problem in the header file will be reflected in the .C file compilation, remember.

转载请注明原文地址:https://www.9cbs.com/read-82924.html

New Post(0)