Disadvantages of dynamic memory allocation legacy arrays: 1 array length must be predetermined, and only a long integer cannot be a variable 2 traditional form definition array the memory program of the array cannot manually release 3 array once defined, The storage space that the system allocates for the array will persist until the function execution ends 4 the length of the array cannot be expanded or scaled up dynamically during the execution of the function. Why you need to dynamically allocate memory for dynamic memory allocation memory examples-- Dynamic array construction static memory allocation and dynamic memory allocation problems using memory across functions #include<stdio.h>void f (void) {int a[5]={0,1,2,3,4}//cannot be released manually to be released after this function is finished}int Main () {f ();} #incliude <stdio.h> #include <malloc.h>int main () {int i=5;/* static allocation *//* allocate eight bytes P store first address number */int *p= (int *) Malloic (4);/* The second int * is cast to int *//* for four bytes but only the first address represents */free (p);//free (p) means that the memory that P points to is released//p itself is not the program ape manual release}/* malloc can only go back to the first address. The memory occupied by */p itself is statically allocated p is the dynamic of the memory referred to
The malloc function learns dynamic memory allocation #include<stdio.h>void f (int *q) {*q=200; if you write Free (p), then the meaningless}int main () {int *p= (int*) in the following main function malloc (sizeof (int.)) *p=10;printf ("%d\n", *p), F (P);/*free (P); * * This is also wrong due to the release of Memory printf ("%d\n", *p); } Zhan Storage structure heap allocation theater payees
Regain the dynamic memory allocation of the C language