// Create a two-dimensional array using new. There are two methods, which are equivalent.

Source: Internet
Author: User

// Create a two-dimensional array using new. There are two methods, which are equivalent.
I:
INT (* P) [10] = new int [5] [10];
II:
Int ** P = New * int [5];
For (INT I = 0; I <5; I ++)
P [I] = new int [10];

// You can create a pointer array in either of the following ways:
I:
Char * pA = new char [4];
For (INT I = 0; I <4; I ++)
{
Pa [I] = new char [8]; // allocate 8 char Element Spaces for each pointer.
}
II:
Char * pa [4]; // you do not need to create a new one.
For (INT I = 0; I <4; I ++)
{
Pa [I] = new char [8]; // allocate 8 char Element Spaces for each pointer.
}


// Delete two-dimensional array
For (INT I = 0; I <5; ++ I)
Delete [] P [I];
Delete [] P;

// Delete pointer Array
I:
For (INT I = 0; I <4; ++ I)
Delete [] Pa [I];
Delete [] Pa;
II:
For (INT I = 0; I <4; ++ I)
Delete [] Pa [I];


All modified

# Include <iostream>
Using namespace STD;

Void main ()
{
// Create a two-dimensional array using new. There are two methods, which are equivalent.
// 1:
INT (* P) [10] = new int [5] [10];
// 2:
Int ** P = new int * [5];
For (INT I = 0; I <5; I ++)
P [I] = new int [10];

// You can create a pointer array in either of the following ways:
// 1:
Char ** Pa = new char * [4];
For (INT I = 0; I <4; I ++)
{
Pa [I] = new char [8]; // allocate 8 char Element Spaces for each pointer.
}
// 2:
Char * pa [4]; // you do not need to create a new one.
For (INT I = 0; I <4; I ++)
{
Pa [I] = new char [8]; // allocate 8 char Element Spaces for each pointer.
}


// Delete two-dimensional array
// For 1:
Delete [] P;
// For 2:
For (INT I = 0; I <5; ++ I)
Delete [] P [I];
Delete [] P;

// Delete pointer Array
// For 1:
For (INT I = 0; I <4; ++ I)
Delete [] Pa [I];
Delete [] Pa;
// For 2:
For (INT I = 0; I <4; ++ I)
Delete [] Pa [I];
}

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.