C / C internal storage management keywords: C , memory allocation, system
Post Time 2001-11-11 21:23:47
Original PAUL_NI
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
Comments on this article
SUNNY2001 (2001-11-13 16:22:46)
Very good, throwing bricks, thank you Heroes! WIND_LQ (2001-11-12 19:58:34)
It is recommended that C initiator takes a closer look, and it will increase its own demonstration capabilities while learning some basic common sense. Wildbaby (2001-11-12 17:26:17)
No relief. I still write it! ! ! These basic teaching primary school students? ? ? Andy_SHOW (2001-11-12 16:23:22)
I believe there are many articles to mention the usage of smart pointers, and STL's auto_ptr is the best example, and most of the NEW allocated memory is automatically released through a Template class. Another way is to implement automatic release of resources based on reference counts by implementing a Reference Wrapper, but overhead is larger than auto_ptr, but the benefits are automatic recycling of different types of system resources (such as Windows systems) Memory, GDI, files, etc. Handle. Regarding how to achieve such a class can refer to the implementation of the _BSTR_t in the VC, although this class is only limited to the package of the COM's BSTR type, but the idea can be applied to the type like Handle. Another better example is the class _com_ptr_t, you should be able to give you a lot of inspiration. In fact, as long as the characteristics of C can avoid memory leakage. However, problems like array are still unavoidable. Thunder_yu (2001-11-12 14:38:36) Oh, use << 就 C ? Call Calmonic management is better.
Topcat (2001-11-12 12:29:01)
Point out some small errors in your article: 1. Memory allocation Failback Returns NULL only when you use the malloc function family, if you use new, the standard C is now throws an exception of a std :: Bad_alloc type. 2. The strcpy () function is only valid for the character pointer, because its end condition is judging (* source) x == '/ 0', if it is used in other types of pointers, hey ... Memcpy should be used. StrCMP also uses the same problem. 3. C is not recommended to use the Malloc / Free family because they can't call the CONSTRUctor and Destructor, but the Malloc / Free has never appeared in your code, this at least half of your topic is not true. 4. In fact, the pointer usage and precautions in C are still very large. More in C is more considering multiple and CTOR / DTOR issues, you can say that the article is completely C's pointer. (In fact, as the array of arrays, C can use Vector and String to maximize pointers).
my comment:
In C , I have never done this. Instead:
#define Safe_Delete (P) (Delete (P), (P) = NULL)
#if _debug
#if defined (new) && defined (mynew) #undef new # Endif
Void * _CDECL Operator New (size_t nsize, lpcstr pfilename, dword dwline) {1. Assign Memory 2 with other memory allocation functions. Do some operations, record memory allocation and allocation. Record file location 4. Tips: _ONEXIT (); how to use it? Show msdn, or ask other people}
Inline void * _cdecl operator new (size_t nsize) {同 同, but no file position}
Inline void _cdecl operator delete (void * pmem, lpcstr pfilename, dword dwline) {1. Use other memory release functions 2. The legitimacy, offshore or the like and output error information 3 are initially determined based on the recorded memory allocation information. Tips in the VC (other compilers don't know): char buff [1024]; sprintf (buff, "% s (% d): error information /N" ,pfilename, dwline ,...);outputdebugstring (BUFF); This allows the VC to jump directly to the error code, of course, limited to the debug run (F5) and the output debug information is set in the MFC Trace. } Inline void _cdecl Operator delete (void * pmem) {1 and 2. 3. The output allocation This memory is the first assignment, then use the VC's condition breakage to track the error code}
#ifdef mynew #define new mynew # else #define mynew new (__ file __, __ line __) # ENDIF
#endif // end_debug
Through the above steps, you can basically eliminate memory usage errors in debug version.