C ++ new Delete dynamically applies for two-dimensional arrays C ++ new Delete dynamically applies for two-dimensional arrays

Source: Internet
Author: User

C ++ new Delete dynamically applies for two-dimensional arrays-microtemperature lunch-blog Garden

C ++ new Delete dynamically applies for two-dimensional arrays

When I was learning C ++, I didn't talk about how to use new Delete to apply for a dynamic two-dimensional array space. I couldn't find a simple one on the Internet.CodeAnd write it as follows:

Take a two-dimensional int array as an example --

Cin> m> N; // M, N indicates the number of rows and columns respectively.

Int ** P;
P = new int * [m];
For (INT I = 0; I <m; I ++)
P [I] = new int [N];

Then we can assign a value to P [I] [J] as a two-dimensional array.

Code explanation:

Point a pointer to a pointer array, and apply a one-dimensional space for each pointer in the pointer array.

LastDon't forget to release:

For (INT I = 0; I <m; I ++)
Delete [] P [I];
Delete [] P;

Introduction complete!

When I was learning C ++, I didn't talk about how to use new Delete to apply for a dynamic two-dimensional array space. I couldn't find the concise code on the Internet, so I wrote the following myself:

Take a two-dimensional int array as an example --

Cin> m> N; // M, N indicates the number of rows and columns respectively.

Int ** P;
P = new int * [m];
For (INT I = 0; I <m; I ++)
P [I] = new int [N];

Then we can assign a value to P [I] [J] as a two-dimensional array.

Code explanation:

Point a pointer to a pointer array, and apply a one-dimensional space for each pointer in the pointer array.

LastDon't forget to release:

For (INT I = 0; I <m; I ++)
Delete [] P [I];
Delete [] P;

Introduction complete!

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.