当前课程知识点:C Programming > Chapter 8 Pointer > 8.11 Dynamic memory allocation and pointer variables pointing to it > 8.11 Dynamic memory allocation and pointer variables pointing to it.mp4
大家好
我是云南大学信息学院丁海燕老师
欢迎走进C语言程序设计课堂
今天我们讲解动态内存分配与指向它的指针变量
什么是动态内存分配呢?
非静态的局部变量
是分配在内存中的动态存储区的
这个存储区是一个称为栈的区域
C语言还允许建立内存动态分配区域
以存放一些临时用的数据
这些数据需要时随时开辟
不需要时随时释放
这些数据是临时存放在一个特别的自由存储区,称为堆区
怎样建立内存的动态分配呢?
对内存的动态分配是通过系统提供的库函数来实现的
主要有malloc,calloc,free,realloc这4个函数
1. malloc函数
其函数原型为
void *malloc(unsigned int size);
其作用是在内存的动态存储区中分配一个长度为size的连续空间
– 函数的值是所分配区域的第一个字节的地址
或者说,此函数是一个指针型函数,
返回的指针指向该分配域的开头位置
例如malloc(100);
开辟100字节的临时分配域
函数值为其第1个字节的地址
注意指针的基类型为void
即不指向任何类型的数据
只提供一个地址
如果此函数未能成功地执行(例如内存空间不足)
则返回空指针(NULL)
2.calloc函数
其函数原型为
void *calloc(unsigned n,unsigned size);
其作用是在内存的动态存储区中
分配n个长度为size的连续空间
这个空间一般比较大,足以保存一个数组
用calloc函数可以为一维数组开辟动态存储空间
n为数组元素个数
每个元素长度为size
这就是动态数组
函数返回指向所分配域的起始位置的指针
如果分配不成功,返回NULL
如: p=calloc(50,4)
开辟50×4个字节的临时分配域
把起始地址赋给指针变量p
3.free函数
其函数原型为void free(void *p);
其作用是释放指针变量p所指向的动态空间
使这部分空间能重新被其他变量使用
p应是最近一次调用calloc或malloc函数时得到的函数返回值
free(p);
释放指针变量p所指向的已分配的动态空间
free函数无返回值
4. realloc函数
其函数原型为
void *realloc(void *p,unsigned int size)
如果已经通过malloc函数或calloc函数获得了动态空间
想改变其大小
可以用recalloc函数重新分配
用realloc函数将p所指向的动态空间的大小改变为size
p的值不变
如果重分配不成功,返回NULL
如:realloc(p,50)
将p所指向的已分配的动态空间改为50字节
以上4个函数的声明在stdlib.h头文件中
在用到这些函数时应当用“#include
指令把stdlib.h头文件包含到程序文件中
下面介绍void指针类型
下面来看例8.30
建立动态数组,输入5个学生的成绩
另外用一个函数检查其中有无低于60分的
输出不合格的成绩
解题思路:
用malloc函数开辟一个动态自由区域
用来存5个学生的成绩
会得到这个动态域第一个字节的地址
它的基类型是void型
用一个基类型为int的指针变量p来指向动态数组的各元素
并输出它们的值
但必须先把malloc函数返回的void指针转换为整型指针
然后赋给p1
程序如下:
{ 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函数
check函数定义如下:
void check(int *p)
定义check函数 形参是int *指针
for(i=0;i<5;i++)
if (p[i]<60)
输出p[i]) 的值
即输出不合格的成绩
例如:输入67 98 59 78 57
输出They are fail:59 57
好了,同学们
动态内存分配与指向它的指针变量
我们就学习到这儿
下节课再见
-1.1 The development and characteristics of C language
--1.The development and characteristics of C language .mp4
--1.1 Self test questions
-1.2 A simple C Language program
--1.2 A simple C Language program.mp4
--【Source program】 Example 1.1 output a line of text Hello, world! "
--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers
--1.2 Self test questions
-1.3 Program, Programming Language and C Program Running Steps
--1.3 Program, Programming Language and C Program Running Steps.mp4
--Running steps of C source program in CodeBlocks
--1.3 Self test questions
-chapter 1 Self-Test
-2.1 The Concept and Description of Algorithms
--2.1 The Concept and Description of Algorithms.mp4
--2.1 Self test questions
-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming
--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4
--2.2 Self test questions
-Chapter 2 Self test questions
-3.1 Simple Structure and Identifier of C Language Program
--3.1 Simple Structure and Identifier of C Language Program.mp4
--【Source program】 Example 3.1 input two integers and output the sum of two numbers.
--【Source program】 Example 3.2 input two integers and output the average value.
--【Source program】 Example 3.3 output the value of the character variable.
--3.1 Self test questions
-3.2 Constants, Variables and Assignments
--3.2 Constants, Variables and Assignments.mp4
--3.2 Self test questions
-3.3 Arithmetic, assignment, increment and decrement operators
--3.3 Arithmetic, assignment, increment and decrement operators.mp4
--3.3 Self test questions
-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--【Source program】 example of sizeof
--3.4 Self test questions
-3.5 Input and Output Examples and Character Input and Output
--3.5 Input and Output Examples and Character Input and Output.mp4
--【Source program】 Example. Find the root of quadratic equation of one variable.
--【Source program】 Example 1. Successively output three characters of BOY.
--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen
--3.5 Self test questions
-3.6 Formatted output printf function
--3.6 Formatted output printf function.mp4
--【Source program】 Example 3.4 output of integer data.
--【Source program】 Example 3.5 real data output.
--【Source program】 Example 3.6 character data output.
--【Source program】 Example 3.7 uses %s to output a string.
--3.6 Self test questions
-3.7 Formatted input scanf function
--3.7 Formatted input scanf function.mp4
--【Source program】 Example 3.8 Input and output integer data
--【Source program】Example 3.9 Input and output single precision and double precision real data.
--【Source program】Example 3.10 input and output character data.
--【Source program】Example 3.11 input and output string.
--3.7 Self test questions
-3.8 Basic Data Types of C Language
--3.8 Basic Data Types of C Language.mp4
--3.8 Self test questions
-Chapter 3 Self test questions
-Operator and expression self test questions
-4.1 Relational operators, logical operators, and if statements
--4.1 Relational operators, logical operators, and if statements.mp4
--【source example】Example 4.1 Find the root of the quadratic equation of one variable.
--【Source program】Example 4.2 Input two real numbers and output them from small to large.
--4.1 Self test questions
-4.2 Switch Statement
--【Source program】Example 4.3 uses switch statement to implement simple menu program
--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.
--4.2 Self test questions
-4.3 Examples of Selection Structural Programs
--4.3 Examples of Selection Structural Programs.mp4
--【Source program】 Example 4.5 determines whether a year is a leap year.
--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.
--【Source program】Example 4.7 calculate the transportation cost for the user.
--4.3 Self test questions
-Chapter 4 Self test questions
-5.1 While and Do-While Statement
--5.1 While and Do…while statement.mp4
--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100
--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100
--5.1 Self test questions
-5.2 For Statement
--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100
--5.2 Self test questions
-5.3 Change the State of Loop Execution and Nested Loop
--5.3 Change the state of loop execution and nested loop .mp4
--【Source program】example 5.4 collecting charity donations
--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.
--【Source program】multiplication table
--【Source program】example 5.6 Output the following 4*5 matrix.
--5.3 Self test questions
-5.4 Loop Structure Program example 1
--5.4 Loop structure program example 1.mp4
--5.4 Self test questions
-5.5 Loop Structure Program example 2
--5.5 Loop structure program example 2.mp4
--【Source program】example 5. Output the following figure.
--【Source program】example6. The problem of 100 chickens and 100 coins
--【Source program】example 7. Find the approximate value of PI
--5.5 Self test questions
-Chapter 5 Self test questions
-6.1 Definition, Reference and Initialization of One-Dimensional Arrays
--6.1 Definition, Reference of One-Dimensional Arrays.mp4
--6.1 Self test questions
-6.2 One-Dimensional Array Programming
--6.2 One-Dimensional Array Programming.mp4
--【Source program】example 1. Fibonacci sequence
--【Source program】example 2. Find the maximum value and the minimum value.
--【Source program】example 3 exchanging array elements in reverse order.
--【Source program】example 4. Bubble sorting.
--6.2 Self test questions
-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays
--6.3 Definition, Reference of Two-Dimensional Arrays.mp4
--6.3 Self test questions
-6.4 Two-Dimensional Array Programming
--6.4 Two-Dimensional Array Programming.mp4
--【Source program】example 1. Get the average score of each subject.
--【Source program】Example 2. The elements of row and column of a 2D array are interchanged
--6.4 Self test questions
-6.5 Definition, initialization and input and output of character arrays
--6.5 Definition, initialization and input and output of character arrays .mp4
--【Source program】Example 6.6 Output a known string.
--【Source program】Example 6.7 Output a diamond.
--【Source program】Example 6.10 Sorting of strings.
--6.5 Self test questions
-6.6 String Processing Function
--6.6 String Processing Function.mp4
--6.6 Self test questions
-6.7 Character Array Programming
--6.7 Character Array Programming.mp4
--6.7 Self test questions
-Chapter 6 Self test questions
-7.1 Function Concept and How to Define and Call Functions
--7.1 Function Concept and How to Define and Call Functions.mp4
--7.1 Self test questions
-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value
--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4
--7.2 Self test questions
-7.3 Declarations of called functions and nested calls to functions
--7.3 Declarations of called functions and nested calls to functions .mp4
--【Source program】Example 7.4 use a function to find the sum of t
--7.3 Self test questions
-7.4 Recursive call to function
--7.4 Recursive call to function.mp4
--【Source program】Example 7.6. How old is the fifth student?
--【Source program】Example 7.7 Use recursion method to find n!
--7.4 Self test questions
-7.5 Array as function parameter (1)
--7.5 Array as function parameter (1).mp4
--【Source program】Example 7.10
--7.5 Self test questions
-7.6 Array as function parameter (2)
--7.6 Array as function parameter (2).mp4
--【Source program】Selection sorting.
--【Source program】Example 7.13 find the maximum of a 3×4 matrix.
--7.6 Self test questions
-7.7 Local and global variables, internal and external functions
--7.7 Local and global variables, internal and external functions.mp4
--【Source program】Example 7.14
--7.7 Self test questions
-7.8 The Survival Period of Variables and the Storage Mode of Local Variables
--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4
--【Source program】Example 7.17
--7.8 Self test questions
-7.9 Storage Categories of Global Variables
--7.9 Storage Categories of Global Variables.mp4
--7.9 Self test questions
-Chapter 7 Self test questions
-8.1 Pointer Concept, Definition and Reference of Pointer Variables
--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4
--8.1 Self test questions
-8.2 Pointer variables as function parameters
--8.2 Pointer variables as function parameters.mp4
--【Source program】Example 8.3. Exchange two data with function call.
--8.2 Self test questions
-8.3 Pointer of array element, operation of pointer and reference of array element by pointer
--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4
--【Source program】Point to array elements with pointer variables.
--8.3 Self test questions
-8.4 Using Array Name as Function Parameter
--8.4 Using Array Name as Function Parameter.mp4
--【Source program】Store array elements in reverse order.
--8.4 Self test questions
-8.5 Reference to multidimensional arrays by pointers
--8.5 Reference to multidimensional arrays by pointers.mp4
--【Source program】Pointer variable pointing to one-dimensional array.
--8.5 Self test questions
-8.6 Referencing strings through pointers
--8.6 Referencing strings through pointers.mp4
--【Source program】Reference string by pointer.
--8.6 Self test questions
-8.7 Character pointer as function parameter
--8.7 Character pointer as function parameter.mp4
--【Source program】Character pointer as function parameter.
--8.7 Self test questions
-8.8 Pointer pointing to function
--8.8 Pointer pointing to function.mp4
--【Source program】Pointer variable pointing to the function.
--8.8 Self test questions
-8.9 Functions that return pointer values
--8.9 Functions that return pointer values.mp4
--【Source program】Intercept substring.
--8.9 Self test questions
-8.10 Pointer arrays and multiple pointers
--8.10 Pointer arrays and multiple pointers.mp4
--【Source program】Sorting of strings.
--8.10 Self test questions
-8.11 Dynamic memory allocation and pointer variables pointing to it
--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4
--【Source program】Dynamic memory allocation.
--8.11 Self test questions
-Chapter 8 Self test questions
-9.1 Define and use structural variables
--9.1 Define and use structural variables.mp4
--【Source program】Structure variable
--9.1 Self test questions
-9.2 Using structure arrays
--9.2 Using structure arrays.mp4
--【Source program】Structure array
--9.2 Self test questions
-9.3 Structure pointer
--【Source program】Structure pointer
--9.3 Self test questions
-Chapter 9 Self test questions
-CodeBlocks Baidu online disk download address
-Final Exam
--final exam