Turn from: http://blog.csdn.net/feixiaoxing/article/details/6827801
Pointers are a nightmare for programmers, as is the case for C-language developers, as well as for C + + developers. Especially in C + +, if you do not pay attention to handle the pointer in the class, it is very easy to problem. If your friends don't believe you can look at the following code:
View Plain class data { int* value; public: data (int num) { if (num > 0) value = (int*) malloc (sizeof (int) * num); } ~data () { if ( Value) free (value); } }; void process () { data m (; ) data p = m; } There is no problem with the above question. You can use your pen to draw a picture on the draft paper on your own first. Then on the machine with the actual environment to verify. Sure enough, the system prompts for an error in memory. And why?。 This is because memory has been released two times. Let's look at the assembly code for the process:
View Plain 21:data m (10); 0040105D push 0Ah 0040105F Lea ecx,[ebp-10h] 00401062 call @ILT +15 (data::d ata) (00401014) 00401067 mov dword ptr [ebp-4],0