C language - malloc develops matrix

Update: When looping to open up memory, loop destruction is also required when destroying memory, here is the code supplement 

Table of contents

Allocate Matrix with Dynamic Memory

Matrix initialization

free matrix memory


Allocate Matrix with Dynamic Memory

More knowledge of dynamic memory is in this blog, this article will introduce the use of malloc to open up the matrix.

malloc is used in C language to dynamically open up memory. Through the malloc function, a series of continuous memory spaces can be applied to the computer.

Because the memory opened by malloc is on the heap, it will not be destroyed with the end of the function's declaration cycle, so when the memory is no longer used, it needs to use free to release the memory. 

The basic way malloc and free are used:

  • void* malloc (size_t size);
  • void free (void* ptr);
  • size is the size of the specified allocated memory, in bytes
  • The unsigned integer type of size_t restricts programmers from misoperation to open up space for negative bytes
  • If the opening is successful, malloc will return a pointer of type void*
  • If the opening fails, a null pointer is returned, so the pointer needs to be checked after malloc
  • When the malloc thing is no longer used, it needs to be released by free, otherwise it will cause a memory leak
  • Both malloc and free need to include the header file <stdlib.h>
     

Example of malloc opening up a two-dimensional matrix:

Expressed in the form of a function: input row row, col column, open up the matrix and return the pointer

double** Make_Matrix(int row,int col)
{
	int i, j;
	double** arr = (double**)malloc(sizeof(double*) * row);
	if (arr != NULL)
	{
		for (i = 0; i < row; i++)
		{
			arr[i] = (double*)malloc(sizeof(double) * col);
		}
	}
	return arr;
}

 The above code can be understood as follows:

  • The computer applies for row double* type memory, and creates a secondary pointer arr to receive the address of this string of memory spaces.
  • The computer has applied for col memory of double type, and assigned its address to the i-th element in arr.
  • Repeat row times, the matrix of row, col and column is developed
  • arr records the address of the first element of the matrix; arr[i] records the address of the first element of each row; arr[i][j] records the elements in the matrix

After the matrix is ​​created, the calculation cannot be performed immediately, and the matrix needs to be initialized.

Matrix initialization

The initialization of the matrix generally refers to initializing all the matrices to 0, and the memory function memset can be used. Memset is only suitable for the clearing operation of the matrix, and assigning values ​​to each element of the matrix requires a for loop to traverse.

memset

The memset function usually initializes the newly requested memory. Its function is to fill a given value in a block of memory. It is the fastest way to clear a large structure or array.

void * memset ( void * ptr, int value, size_t num );

  • ptr is the starting position
  • value is the value to be assigned, it can only be a character, 0 or -1
  • num is the number of assignments, the unit is byte
void Init_Matrix(double**arr)
{
	int i;
	int row= (int)_msize(arr) / (int)sizeof(double*);
	for (i = 0; i < row; i++)
	{
		memset((arr[i]), 0, _msize(*arr));
	}
}

In the above code:

  • Set all the memory pointed to by the pointer in each arr[i] to 0
  • _msize(*arr) indicates how many bytes a line has
  • If arr is passed to memset, memset will set the original storage pointer element in arr to 0, causing a memory leak

traditional traversal

void Init_Matrix(double** arr)
{
	int i, j;
	int row = (int)_msize(arr) / (int)sizeof(double*);
	int col = (int)_msize(*arr) / (int)sizeof(double);
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			arr[i][j] = 0;
		}
	}
}

free matrix memory

Because the matrix is ​​created cyclically when it is created, it still needs to be cyclically destroyed when it is destroyed. The code is as follows:

void free_Matrix(double** src,int row)
{
    assert(src);
    for(i = 0;i < row; i++)
    {
        free(src[i]);
    }
}

Guess you like

Origin blog.csdn.net/why1472587/article/details/128137543