See C + + (pointer copy) from a compilation perspective

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.