Dynamically defining arrays

Source: Internet
Author: User
Tags array definition

First: Why do we need to dynamically define arrays?
This is because, in many cases, the length of the array is not known in advance during the precompilation process, and must be dynamically given when the program is running
The problem, however, is that C + + requires that you explicitly specify the size of the given array when you define an array, otherwise the compilation does not pass

such as: int array[5]; correct

int i=5;
int array[i]; Error because in the compile phase, the compiler does not know what the value of I is

So how do we solve an array that defines an unknown length?
The answer is: New Dynamic definition Array

Because new is used to dynamically open up space, of course, it can be used to open an array space.

In this way, the following statement:
int size=50;
int *p=new Int[size]; is right.

But can a two-dimensional dynamic array also be defined like this?

int size=50,column=50;
int (*p) [column]=new int [Size][column]

Such a statement, the compiler does not pass, why?
First new Int[size][column] is determined dynamically when it is generated, so it's not wrong
So that's the int (*p) [Column], which is a question, why is it wrong, that's because, this is a definition statement, and the definition statement is compiled by the compiler, and when the compiler runs here, it finds that Column is not a constant and therefore cannot be compiled. The reason why the compiler thinks that column is not a constant is that because of the compile phase, the compiler plays a syntax error, and the pre-allocated space does not execute the program, so the assignment statement is not executed (just check the error on the statement and allocate space), so the compile phase, it will assume that the column is a variable. So the above two-dimensional array definition is wrong and it cannot be compiled.

Change to this:
int size=50
int (*p) [50]=new int [size][50]
Was right.

Thus, this dynamic allocation of arrays, only one-dimensional array space is really dynamically allocated.
But how to really dynamically allocate a two-dimensional array, that is, if the column can not know beforehand, how to deal with it?

The above dynamic allocation has not satisfied our requirements, because the above dynamic allocation only to one-dimensional array is really dynamic, for a two-dimensional array, must be compiled prior to know the length of each column of the two-dimensional array, and this length in many cases is not known beforehand, so we have to combine other methods to solve the problem.

Since one dimension is a real dynamic assignment, we use this feature to define an array of pointers.

int **p= New int*[size];//defines an array of pointers
int *p[5];//If you know that the number of rows in a two-dimensional array is 5

Each pointer in the pointer array is then assigned a one-dimensional array space, which dynamically defines the two-dimensional array

In fact, I think the main purpose of pointer arrays is to dynamically define multidimensional arrays

for (int i=0;i<size;i++)
{
P[i]=new Int[column];
}

Once run, a two-dimensional array is successfully established dynamically

-----------------------------------

Example:

size = 6;

Column =5

int **p=new Int*[size];

for (int i=0;i<size;i++)
{
P[i]=new Int[column];
}

The resulting dynamic array is as follows:




Finally, because the new is called, never forget to delete the resource

Here is the Delete method:

for (int i=0;i<size;i++)
{

Delete [] p[i]; To add [] in front of the pointer, or release only the space occupied by the first cell referred to by p[i]
}

delete [] p; Finally, don't forget to release the open array of pointers: "

Dynamically defining arrays

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.