C + + Two-dimensional array dynamic memory allocation

Source: Internet
Author: User
Tags int size

Allocation of memory for two-dimensional arrays and two-dimensional pointers

Here it is preferred to say the memory allocation of one-dimensional pointers and one-dimensional arrays.

One-dimensional:

Array: shaped like int a[5]; here a one-dimensional array A is defined, and the number of elements in the array is 5, where a is the overall representation of the five elements, that is, through a we can find the five elements. Note: A is the first address of the element that represents the array. &a are the addresses that represent the array, although their values are the same.

pointer: int *p = NULL; here p is a pointer, which points to the calculation

An int type of memory is stored inside the machine. p = A; let P be equal to the address of the first element of the array you just requested. So through p we can also find the 5 elements so p[i] is the same as a[i.

Attention:

1:int *p = NULL; The size of p in the 32-bit machine is 4, even if p=a, and then P's sizeof (p) is still equal to 4.

2: After the declaration, the array must be allocated memory for initialization. The pointer, in general, allocates the memory it points to dynamically.

3: Do not confuse pointers and arrays, pointers are pointers, arrays are arrays, but arrays can be converted to pointers under certain conditions. Do not confuse the pointer and the array. (for example, the pointer has a ++,--operation and the array is not possible).

Dynamic memory allocation for one-dimensional pointers:

int *p = NULL;

p = new Int[n];

Don't forget the delete.

delete [] p;

p = NULL;

Memory allocation for two-dimensional arrays

int a[2][3]; This allocates an array of 2x3=6 int size. The second dimension of a two-dimensional array 3 cannot be omitted.

The memory of the two-dimensional array is also a contiguous address within the computer, except that each 3 elements form a one-dimensional array a[i], where a[i] represents the address of the first element of the array with the Dimension 3. So A[i][j] 's visit with A[i] is clear. Here A[i] is actually the address of the first element of a one-dimensional array.

For two-dimensional arrays, we usually use one-dimensional pointers for arguments, such as:

1 #include <iostream> 2 void Test (int *p) 3 {4 for     (int i = 0;i<3;++i) 5     {6 for         (int j = 0;j<3;++ J) 7         {8             std::cout<<* (P+3*I+J);//one-dimensional processing 9         }10     }11}12 int main (void) (     a[3][3]={int) 1,2,3,4,5,6,7,0,0};15     Test ((int*) a);     Two-dimensional arrays are treated as one-dimensional processing     system ("pause");     0;18}

These must be very clear in the book.

C + + dynamic memory allocation for two-dimensional arrays.

Dynamic array allocation for two-dimensional pointers: assignment of two-dimensional pointers to arrays of pointers

int **p;

1 #include <iostream> 2 int main (void) 3 {4     int **p = NULL;       Here applies a 3x4 two-dimensional array 5     p = new int *[3];     Assigns a one-dimensional pointer, assigning one-dimensional pointers of three int* types.  6 for     (int i = 0;i < 3, ++i) 7     {8         p[i] = new Int[4]; 9     }10 for     (int i = 0; i < 3; ++i) 11
   {12 for         (int j = 0; j < 4; ++j) (             p[i][j] = i*j;15             std::cout<<p[i][j]<< ""; 16< c15/>}17         std::cout<<std::endl;18     }19     for     (int i = 0;i < 3;++i)    //release     {22         Delete [] p[i];23     }     Delete [] p;25     System ("pause");     return 0;27}

Dynamic memory allocation for pointer arrays

The dynamic memory allocation of a pointer array requires only allocating memory to the array element pointers of the pointer array, which is less than the allocation of two-dimensional pointers.

1 #include <iostream> 2 int main (void) 3 {4     int *a[3];    Apply a pointer array containing three int* types 5                   //Unlike two-dimensional pointers, here array A does not need to manually request memory 6 for     (int i = 0;i < 3;++i)  //apply for a 3x4 space 7     {8         a[ I] = new int[4]; 9     }10 for     (int i = 0; i<3; ++i) One     {         k for (int j = 0; j<4; ++j) (+ a[i][j)             = I*j;15
   std::cout<<a[i][j]<< "";         }17         std::cout<<std::endl;18}19 for     (int i = 0;i <3; ++i)  //release three pointer elements respectively, due to the array in the stack area, will automatically release the     {x         delete [] a[i];23     }24     System ("pause"); 25     return 0;26}

Dynamic memory allocation for array pointers

An array pointer is a pointer to an array, which is simply pointing to an array as a whole, so you can apply a memory address directly when you assign it. is similar to the static assignment of a two-dimensional array.

1//Karllen 2 int main (void) 3 {4     int (*a) [4];     Here 4 is the dimension of the second dimension, the increment of a has a base of 4 int5     a = new int[3][4];      6     Delete []a;7         a = null;8     return 0;9}

The most used is the above several methods. There are several ways to allocate memory for two-dimensional arrays at once. I am also just touching these things, I hope you can put forward the wrong place. Share!!!

C + + Two-dimensional array dynamic memory allocation

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.