C program in terrible field guide

First, the question point
pointer C language is a very powerful feature, but also a function it is easy to make mistakes, the wrong hands, the light just reported a mistake, in serious cases may the entire system collapsed. The following is a wrong method we use when writing C programs, often encountered, perhaps in your study and work is so used, is very dangerous. The procedure of Example 1:
C program in terrible field guide

FIG procedure of Example 1
this process is relatively simple, str1 at the memory storage area for a character string "123" to "123" assigned to the memory area pointed str2, an alarm will be given when compiling:
local variable 'str2' Used without having been initialized
mean to say, "str2" this variable is not initialized. We can ignore this warning and continue the process
sequence, but "str2" in the definition did not give the initial value, is a dangling pointer, the result of running can be very scary. We analyze in detail below under the wild pointer scary.

Second, wild pointers terrible
our program
strcpy (str2, str1);
the printf ( "is pointed in str2% s", str2);
two lines commented, and run the program to see if the value of the output str2 how many.
Results are as follows:
C program in terrible field guide
Visible str2 is assigned a value 3435973836,3435973836 system is a memory address, which is a pointer str2 point to this memory, this memory data stored in the data might be some other programs, such as saving the "hello ! ", as shown in FIG world 2, what data may not.
C program in terrible field guide

2 important data to other programs

If this memory to save important data with other programs, by strcopy function "123" to the copy of this memory, which is important to modify this data, this memory to save data into a "123lo world!", As As shown in Figure 3, other programs may just bounced off!
C program in terrible field guide

FIG 3 other important data is rewritten program

Third, avoid dangling pointers methods
to prevent the wild pointer to bring disaster, it is recommended to define a pointer when the initial value, such as "NULL", meaning not point to any memory address. Then use malloc function to allocate a memory space pointer. Modified program shown in Figure 4:
C program in terrible field guide

FIG avoid changing method program pointer field

Initialization "NULL" when defining str2, so str2 does not point to any memory. Through the malloc function, the application period of empty memory areas, i.e. there is no memory area used by the program, so that this point str2 vacant memory area, shown in Figure 5, then at this time, "123" assigned to this empty memory area so safe. Finally, active release program out of this memory area, so str2 again does not point to any area.

C program in terrible field guide
FIG 5 system allocates memory areas

Run results shown in Figure 6:
C program in terrible field guide

6 operating results

The results can be seen, the system does not allocate any memory address used by other programs are "2,428,680."

Guess you like

Origin blog.51cto.com/poopoo/2421139