C + + Array deallocation issues

Source: Internet
Author: User

I met a small problem when I was doing a 2-question today:

The code I modified was as follows:

1 //Proj1.cpp2#include <iostream>3 using namespacestd;4 5 classMyClass {6  Public:7MyClass (intlen)8    { 9Array =New int[Len];TenArraySize =Len; One        for(inti =0; i < arraySize; i++) AArray[i] = i+1; -    } -  the~MyClass () -    { - //ERROR **********found********** - Delete array; +    } -  +    voidPrint ()Const A    { at        for(inti =0; i < arraySize; i++) - //ERROR **********found********** -cout << Array[i] <<' '; -  -cout <<Endl; -     } in Private: -    int*Array; to    intarraySize; + }; - intMain () the { * //ERROR **********found********** $MyClass obj (Ten);Panax Notoginseng  - obj. Print (); the    return 0; +}

No problem after running successfully, but the 18th line gave me the wrong.

I've always thought that array[] and array are not the same, but I've looked at the relevant information:

    • Array represents this number, which is the equivalent of a pointer to the first element (&a[0]), that is, to the first address of the array, the other elements of the array can be obtained by the displacement of the arrays, at this time the advance is in the array of individual element type units
    • Array[] Represents the address of the variable instead of the array[], but instead takes the address of the array element. Although array[] and array have the same memory address, but they do not have the same meaning, it represents the entire array, its advanced unit is the entire array of bytes length

And about delete and delete[] My understanding also has a misunderstanding:

http://blog.csdn.net/shandaliuyan/article/details/5930719

The final use principle is: New and delete, new[] and delete[] corresponding to use, array selection using delete[].

C + + array deallocation problem

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.