[Reserved] C ++ two-dimensional dynamic arrays memset () function initializes

Source: https://blog.csdn.net/longhopefor/article/details/20994919

Let us talk about memset function:

void * memset (void * s, int c, size_t n)
action: the s memory space has opened the first n-byte value to values, c.

Connotation Memset (): the memory section is used to set all of a character, generally used in the definition of strings is initialized to 'or' / 0 '; Example: char a [100]; memset (a, '/ 0', sizeof (a));

Memset () function is used to initialize the memory space. Such as:
char STR [100];
Memset (STR, 0, the sizeof (STR));

 

Let us talk about dynamic allocation and initialization of a one-dimensional array:

Dynamically allocated one-dimensional array, simple initialization and withdrawal, as follows:

Dynamic allocation: int * array = new int [10]; // dynamic allocation space 10

初始化:memset(array,0,sizeof(array));      或者memset(array,0,10*sizeof(int));

Undo: delete [] array;

 

Here to talk about a two-dimensional array:

Two-dimensional array (n rows and m columns) using a new dynamic allocation is actually equivalent to the n m-ary dynamically allocated arrays, but we can not blindly dynamic allocation method according to the one-dimensional array operation. MSVC has not so user-friendly, concrete should do:

int **array;
array=new int *[10];
for(int i=0;i<10;i++)
         array[i]=new int [5];


   The above operation completes a two-dimensional array array of 10 rows and 5 columns [10] [5] The dynamic allocation, we can see the first dynamically allocated first address pointer array pointer to a unit 10 ** array, dynamic and then to the first address of each of its traverse, while the complete array of a dynamic partition allocation unit 5, and to the first address * array [i], and thus finally completed the two-dimensional array array [10] [5] of distribution. And so on, we can get an array of three-dimensional dynamic allocation method as well as multi-dimensional.

Two-dimensional array initialization: If the one-dimensional array initialization will find ways to copy over to a two-dimensional array of dynamic allocation does not apply. As can be seen from the above description memset can only act on a one-dimensional array * array, so the best way is dynamically allocated two-dimensional array and combine, new a, memset a. Specific wording as follows:

int **array;
array=new int *[10];
for(int i=0;i<10;i++)

{
         array[i]=new int [5];

        memset(array[i],0,5*sizeof(int));

}

 

Two-dimensional array of revocation:


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

     delete[] array[i];

    array [i] = NULL; // Do not forget, p [i] does not automatically point to free up space after the NULL value, will keep in the same place, just release the memory of it, nothing more.

}

delete [] array;

array=NULL;


 

Finally, a description unusual distribution:
int (* P) [. 4] = new new int [. 3] [. 4];


Explanation: Some beginners may think that this is desirable, but also the use of an array of new distribution, it can be dynamic, that your ideas are wrong, its distribution must have the support of the outermost layer of const
int x = 3, . 4 = Y;
int (* P) [Y] = new new int [X] [Y]; // error, Y must be const.
So in this way can not achieve the real purpose of dynamic allocation of two-dimensional array, only the equivalent of a semi-automated distribution.


Last reminder: Do not have int * p = new int [4] [2]; such a mistake writing.

 

Guess you like

Origin www.cnblogs.com/jiading/p/11101541.html
Recommended