Monday, October 25, 2010

Audio Bible C language compiler raises the question



Basic interpretation
This section focuses on the following C compiler, triggered by the characteristics of two series of common programming problems.
On the C files are compiled:
C program consists of several small programs (. C file), the compiler compile these were a few small programs, then the program will link them together to form a target code. Since the compiler can only compile a file each time, so it can not immediately check with the source files need to be found a few errors.
The function parameters and return values with a temporary variable
C compiler, would function to establish the parameters of the provisional parameters, which may be implicit return value passing a pointer. Because these temporary variables implied the existence of so in some cases, especially when there is a pointer, will trigger a series of problems.
C file contained in the header file and the C language with compiler

C language header files are included and. C files compiled with the first issue of the document will be reflected. C file compilation.
Questions: C files are compiled

I have an array of a custom in f1.c in, but I would like to calculate it in f2.c number of elements, sizeof can be used to achieve this purpose?

Answer and Analysis:

The answer is no, you have no way to achieve the purpose, essentially because the sizeof operator is in the "compile-time (compile time)" works, and C language compilation unit is each individual. C files to compile (other languages also the case). Therefore, sizeof identify a source file with a size of the array, but defined in another source file for the array it can do nothing, because it is already "run-time (run time)" can be sure about everything.

One thing to want to do, always a way, the following three options to provide solution to this problem:

1), define a global variable, it remember the size of the array, in another. C file, we pass to access this global variable to get the array size of the message (if there is a disease worth the ^_^)銆?br />
2), in a. H file size of the array using macro definitions, such as # define ARRAY_SIZE 50, then the two source files are included in this. H files to get through the definition of direct access to ARRAY_SIZE different. C files The size of the array.

3), set the last element of the array as a special value, such as 0, -1, NULL, etc., and then we pass through the array to find the end of this particular element, and thus determine the length of the array (low efficiency of this approach is simple-minded, ).

Question: passing pointer function return value implied

The following code can work, but at the end of the program will generate a fatal error. What are the reasons?

struct list
(
char * item;
struct list * next;
)

main (argc, argv)
(
...
)

Answer and Analysis:

The reason is simple, a little note that is not difficult to find the definition of the structure list in parentheses behind the right flowers can add a semicolon to solve this problem:

struct list
(
char * item;
struct list * next;
); / / Missing semicolon can not do this!

Well, the problem is solved, but, you know what this error actually resulted in a fatal problem? The problem is not so simple on the surface of, OK, let's look at the truth behind things.

First look at the following code:

VOID Func (struct my_struct stX)
(
.......
)
struct my_struct stY = {...};
Func (stY);

When you call the function Func of time is to structure the value of the variable stY a copy to the call stack, thereby passed as parameters to the function FUNC, this is called the C language the parameters passed by value. I believe that you must be very clear, then you should know: If the return value is a structure variable, then the function should be how to value it returns to the caller? Consider the following code:

struct my_structFunc (VOID)
(
.......
)
struct my_struct stY = Func ();

At this point the return value of function Func is a structure type of value, this return value of Pi in memory of a dark terror of 鍦版柟 and arranged a pointer to the place (provisionally called "mysterious pointer"), but the pointer will by the C language compiler as a hidden parameter to the function Func. When the function Func to return, the compiler generated code will hide this from the memory area pointed to the value of copy to the return structure stY in order to complete the structure of the variable value back to the caller.

Do you understand the above mentioned stuff, then the real cause of the problem today, also ready to come out of the:

Because the definition of struct list {...} does not add a semicolon followed, leading to the main function main (argc, argv) understood by the compiler return value structure is a function of variables, so expect argc and argv in addition to the first outside three parameters, namely, that we mentioned above, the incoming implied a "mysterious pointer." But, you know, here is the main function of the function, main function of the parameter is the boot code from the program (startup code) provided. The startup code of course, that the main () should only be born with two parameters, to "mysterious pointer" and, of course not, this way, main () when given a free hand to go back to access the call stack that it does not exist The third parameter (the mysterious pointer), this has led to the illegal access, resulting in a fatal problem. This is the real source of the problem.

Recommendation:

1), try to structure a pointer variable, not the structure itself as a function of parameters, or memory copy function call overhead from time to time small, especially for those who call frequently, the situation of a large structure.

2), the structure must be defined in the back of a semicolon, after the above paragraph about my major, I am sure you will not commit the same error



Problem: the parameters of the function the compiler will implicitly create a temporary copy

Test run the following function does what kind of results?

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 Lin Rui, "C / C + + high-quality programming guide," the above example, use of them about.

This call will produce the following two consequences:

1), can output hello

2), memory leak

Another related question:

Will run the Test function, what kind of results?

void GetMemory (char * p)
(
p = (char *) malloc (100);
)

void Test (void)
(
char * str = NULL;
GetMemory (str);
strcpy (str, hello world);
printf (str);
)

Answer and Analysis:

Serious consequences, the result is a crash running, by running the debugger we can see, after GetMemory, Test function of str is still NULL. One can imagine that a call

strcpy (str, hello world);

Program is bound to collapse trouble.

Analysis:

C compiler will always make for the function of each parameter temporary copy of a copy of the pointer parameters is p _p, compiler allows _p = p. If the function body of the application to modify the content of the _p, p parameters on the content of lead changes accordingly. This is the pointer can be used as output parameters reasons. In this case, _p apply for a new memory, just _p memory address within the meaning of change, but the p stuff. Therefore, the function GetMemory not output anything, if you want to output dynamic memory, use pointer to pointer, or use a pointer pointing to references.

Question: header file and include it. C files compiled with Q

The following code is very short, it seems there is no problem, but the compiler will report an error, what problems may arise where?

# Include someheader.h
int myint = 0;

Answer and Analysis:

Do not stare at int myint = 0; look, this one is a C language assignment should be the most simple statements, the problem will definitely not out on it, the problem may only appear in the someheader.h, the most common is the header file The last line of the statement (function Ye Hao, variable means) did not use a semicolon; the end, then the compiler will combine it myint variables to consider, naturally wrong.

The main problem is to remind you that when the positioning of ideas to expand, it may have to consider whether the header file contains a problem.

Conclusion: The header files are included and. C files compiled with the first issue of the document will be reflected. C files to compile in, and remember.







相关链接:



FrontPage create thumbnails and images with overlapping effects



Symantec Said the new storm worm detected



Crossroads 3G



5 easy ways to prolong laptop life



Useful tool for Snippet COMPILER



Under The Iceberg



WMV To MOV



Review Network Monitoring



MPEG4 to WMV



Sail a meteoric rise THROUGH media and public relations



Flash To MPEG



Kingsoft vice president said the leaked report seen those INTERNAL to track down leaks



Recommend Source Editors



Refrain from the trap into the concept of selection need to know ourselves and HR



Yuan Meng: Ubuntu 9.10 new version of the debut



No comments:

Post a Comment