C / C internal deposit management
Welcome everyone to come to this most programmers, have a little thunder. The great Bill Gates, this century, has gone to EVERYBOTY - BILL GATES 1981 believes that programmers often write some programs about memory allocation and use, and have to die. Feeling (Of course I refer to the feeling of the debugger, may exaggerate some!) Common memory allocation and use error 1) The application and assignment of memory is not successful, but the programmer uses it. Some newcomers often make this mistake, they will not pay attention to memory without allocation. It is determined whether the value of the pointer is NULL to effectively avoid this error. 2) The distribution of memory has been successful, but it is directly used without initialization. The first is the problem, many people don't initialize such habits before using the pointer, but this habit is very important, I hope everyone must force themselves. The second is that the default value of the memory of the memory is 0, so that there is no reason, and the value after memory allocation is uncertain. 3) The above two works have been done (successfully applied and initialized), but the operation is getting more. 4) Apply for memory and use it but forget the release, causing memory leakage. Such errors can be described as a malignant tumor, it will not immediately want to live, but it will slowly swallow your system resources until your procedure is completely easter. 5) Alley releases memory carefully, but use it again. Since the program is complex or error in the call order, this may result in the above error. Pointer - a great double-edged sword I really admire the invention of the invention, he is too great. This is a great achievement that can be described in such a way that it is a great achievement using such a simple way to describe the complex memory structure. However, the pointer is like a soldier like a weapon. It can be very powerful, and it is harmful to others. Let me talk about the difference between the pointers and arrays. The array name corresponds to a memory, its address, the capacity is not variable in its life cycle, and only the array content is variable. The pointer can point to any type of memory at any time, and its feature is "change". The pointer is far more flexible than the array, but it is also more dangerous. An array name cannot be assigned and compared. If you assign an array A to array B, you cannot use the assignment statement B = A, which will make the compiler generate errors. The standard library function strcpy must be used for assignment. In the same manner, to compare whether the contents of A and B are the same, the ordinary logic can not be used to determine if the library function strcmp is applied. // array ... char a [] = "hello"; char b [100]; struct (b, a); // b = a is Wrongif (strcmp (b, a) == 0) // IF (B) == a) Is Wrong Cout << B << endl; // pointer ... int LEN = Strlen (a); char * p = (char *) Malloc (Sizeof (Char) * (Len 1)); strcpy (p, a); if (strcmp (p, a) == 0) cout << p << endl; free (p); one thing must be pointed out when calculating memory capacity, that is, SizeOf calculation array It is calculated to calculate its actual memory capacity, while calculating a pointer is always 4 bytes. C is never a way until the memory capacity refers to the pointer, unless you remember it when you apply.