C + +-new Delete high-dimensional array summary

Source: Internet
Author: User

Reference: http://www.cnblogs.com/beyondstorm/archive/2008/08/26/1276278.html

Http://www.cnblogs.com/platero/archive/2010/12/18/1910057.html

The dynamic application and release of high-dimensional arrays is similar to the two-dimensional arrays, so only the dynamic application and release of the two-dimensional arrays are shown here.

Let's start with a Volkswagen version:

1#include <iostream>2 3  using namespacestd;4 5 intMainvoid)6 {7         int**p;8 9p =New int*[3];Ten  One          for(inti =0; I <3; i++) AP[i] =New int[4]; -  -          for(inti =0; I <3; i++) { the                  for(intj =0; J <4; J + +) -P[I][J] = i+J; -         } -  +          for(inti =0; I <3; i++) - delete []p[i]; +  A delete []p; at  -         return 0; -}

Then there are various new:

  

1.
A (*GA) [n] = new A[m][n];
...
delete []ga;
Disadvantage: n must be a known
Advantages: Call intuitive, continuous storage, program concise (after testing, destructors can be called correctly)

2. a** ga = new A*[m];
for (int i = 0; i < m; i++)
Ga[i] = new A[n];
...
for (int i = 0; i < m; i++)
delete []ga[i];
delete []ga;
Cons: Non-sequential storage, cumbersome procedures, GA for a** type
Pros: Call intuitive, n may not be known

3. * ga = new A[m*n];
...
delete []ga;
Cons: Call not intuitive
Advantage: Continuous storage, n may not be known

4. Vector > GA;
Ga.resize (m); These three lines can be used
for (int i = 1; i < n; i++)//
Ga[i].resize (n); //
...

Disadvantages: Non-continuous storage, debugging is not convenient, the compiler speed down, program expansion (actual speed difference is not small)
Advantages: Call intuitive, automatic destruction and free memory, you can call the STL-related functions, dynamic growth

5. Vector ga;
Ga.resize (M*n);
The combination of method 3,4


6.2 of the improved version
a** GA = new A*[m];
Ga[0] = new A[m*n];
for (int i = 1; i < m; i++)
Ga[i] = ga[i-1]+n;
Advantages: Continuous storage, n can not be known, easy to deconstruct, guess just delete [] GA;

Problem:

1. How do I declare and initialize a multidimensional array?

2. Is it possible to dynamically allocate multi-dimensional arrays, and how to properly deconstruct them?

3. How do I understand these operations?

C + +-new Delete high-dimensional array summary

Related Article

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.