CC ++ internal deposit management

zhaozj2021-02-17  32

Welcome everyone to come to this most programmers, have a little thunder. The great Bill Gates, this century, has gone wrong:

640k ught to be enough for everybody - bill Gates 1981

I believe that programmers often write some programs about memory allocation and use, and have a feeling that is not as dead as death. (Of course I refer to the feeling of debugging, may be exaggerated!)

Common memory allocation and use errors

1) The application and distribution of memory have not been 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. 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];

STRCPY (B, A); // b = a is WRONG

IF (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);

When calculating the memory capacity, it is necessary to point out, that is, the SIZEOF calculation array is calculated to calculate its actual memory capacity, while calculating the 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. How does Free and DELETE deal with the pointer?

Programmers know that they are used to release the application, but few have noticed that the pointer does not change. You can use single-step tracking in VC, you will be surprised to find that when the pointer P is called Free, its address value has not changed, but the other value of the inner memory in the address has become garbage, " P "but the memory pointing to it. Remember, be sure to set the value of P in the first time to null, otherwise you will make it a meaningful pointer to use it (when others use this pointer, it is determined whether the value of the pointer is NULL, if not It will make it meaningful for null).

Char * p = (char *) malloc (100);

STRCPY (P, "Hello");

Free (p); // The address of "p" is not change.

.

IF (NULL! = P) // IT Will Return True

STRCPY (P, "world"); // Wrong !!!

The following is two points, let everyone prevent the above situation:

1) The pointer declares immediately to initialize it immediately. Because the default value of the pointer is random, it must be assigned NULL and then use.

2) Be sure to assign the pointer to NULL after calling Free and Delete. The reason has been mentioned above, and will not be described again.

This article first analyzes common errors that will occur with memory. Then discusses the most critical part of the most critical part of the memory usage - the usage and techniques of usual not to pay attention. These are all experiences I usually have accumulated in the project, I hope to help everyone (especially those who are still struggling in the suffering of memory). What experiences and problems need to be exchanged, please mail me. Paulni@citiz.net

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

New Post(0)