C language pointer operation (8) * dynamic memory allocation and pointer variables pointing to it

insert image description here

The biggest reason for learning is to get rid of mediocrity. One day earlier, there will be more splendor in life; one day later, one day more mediocrity. 

study diary

Table of contents

study diary

ForewordEdit

1. What is the dynamic allocation of memory

2. How to establish dynamic allocation of memory 

Use malloc function to open up dynamic storage area       

 Use calloc function to open up dynamic storage area

Use the realloc function to reallocate dynamic memory 

Use the free function to release the dynamic storage area 

Three, void pointer type 

 Notice

example

C language pointer manipulation


foreword

          The pointer is divided into eight small parts here, namely "pointer variable", " array referenced by pointer", "*referred to multidimensional array by pointer", "string referenced by pointer", "*pointer to function", "* Functions Returning Pointer Values", "* Arrays of Pointers and Multiple Pointers", "*Dynamic Memory Allocation and Pointer Variables to It".  

1. What is the dynamic allocation of memory

        Global variables are allocated in a static storage area in memory, and non-static local variables (including formal parameters) are allocated in a dynamic storage area in memory. This storage area is an area called a stack (stack). In addition, the C language also allows the establishment of memory dynamic allocation areas to store some temporary data. These data do not have to be defined in the declaration part of the program, nor do they need to be released until the end of the function, but can be opened at any time when needed. release at any time. These data are temporarily stored in a special free storage area called the heap (heap) area. You can apply to the system for a space of the required size as needed. Since they are not defined as variables or arrays in the declaration part, these data cannot be referenced by variable names or array names, only by pointers.

2. How to establish dynamic allocation of memory 

Use malloc function to open up dynamic storage area       

 void*malloc(unsigned int size)

        The function is to allocate a continuous space with a length of size in the dynamic storage area of ​​the memory. The type of the formal parameter size is defined as an unsigned integer (negative numbers are not allowed). The value of this function (that is, the "return value") is the address of the first byte of the allocated area, or in other words, this function is a pointer function, and the returned pointer points to the first byte of the allocated area.

        The base type of the pointer is void, that is, it does not point to any type of data, and only provides a pure address.

 Use calloc function to open up dynamic storage area

 void*calloc(unsigned  n,unsigned size)

        The function is to allocate n continuous spaces with a length of size in the dynamic storage area of ​​the memory. This space is generally large enough to store an array. The calloc function can be used to open up dynamic storage space for one-dimensional arrays, n is the number of array elements, and the length of each element is size. This is a dynamic array.

Use the realloc function to reallocate dynamic memory 

 void*realloc(void *p,unsigned int size)

        If the dynamic space has been obtained through the malloc function or the calloc function, and you want to change its size, you can use the realloc function to reallocate it. Use the realloc function to change the size of the dynamic space pointed to by p to size. The value of p does not change. Returns NULL if reallocation is unsuccessful.

Use the free function to release the dynamic storage area 

 void free(void *p)

        The function is to release the dynamic space pointed to by the pointer variable p, so that this part of the space can be reused by other variables. p should be the function return value obtained when calling the calloc or malloc function last time. 

Three, void pointer type 

        C99 allows pointer types with base type void. You can define a pointer variable whose base type is void (that is, a variable of type void*), which does not point to any type of data. When assigning its value to another pointer variable, it is type-converted by the system to make it suitable for the type of the assigned variable.

int *pt;
pt=(int *)malloc(100);	//malloc(100)是void *型,把它转换为int *型

 Notice

        Do not understand "pointing to void type" as data that can point to "any type", but should be understood as "pointing to an empty type" or "not pointing to a certain type".

example

        Create a dynamic array, input the grades of 5 students, and use a function to check whether there are any students with a score lower than 60, and output unqualified grades. 

#include <stdio.h>
#include <stdlib.h>				//程序中用了malloc函数,应包含stdlib.h
int main()
{	void check(int *);				//函数声明
	int *p1,i;						//p1是int型指针
	p1=(int *)malloc(5*sizeof(int));	//开辟动态内存区,将地址转换成int *型,然后放在p1中
	for(i=0;i<5;i++) 
		scanf("%d",p1+i);			//输入5个学生的成绩 
	check(p1);					//调用check函数
	return 0;
}

void check(int *p)					//定义check函数,形参是int*指针
{	int i;
	printf("They are fail:");
	for(i=0;i<5;i++)
		if(p[i]<60) printf("%d ",p[i]); 	//输出不合格的成绩 
	printf("\n");
}

C language pointer manipulation

C language pointer operation (1) pointer variable

C language pointer operation (2) referencing arrays through pointers

C language pointer operation (3) *Referring to multidimensional arrays through pointers 

C language pointer operation (four) referencing strings through pointers

C language pointer operation (5) * pointer to function

C language pointer operation (6) * function that returns a pointer value

C language pointer operation (7) * pointer array and multiple pointers 

C language pointer operation (8) * dynamic memory allocation and pointer variables pointing to it

Guess you like

Origin blog.csdn.net/m0_63794226/article/details/126617479