Disadvantages of dynamic memory allocation legacy arrays: 1 array length must be predetermined and can only be a long integer cannot be a variable 2 traditional form defined 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 runs at the end of the 4 array length cannot be expanded or scaled up dynamically in the course of the function's operation. 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 release the system after the function has run out}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 * cast to int *//* represents four bytes but only the first address represents */free (p),//free (p) indicates that the memory that the P points to is released//p itself is not manually released by the programmer}/* The only thing that malloc can do is to return 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 this is write free (p), then the meaningless}int main () {int *p= (int*) in the main function later malloc (sizeof (int.)) *p=10;printf ("%d\n", *p), F (P);/*free (P); * * This is also wrong because memory printf has been freed ("%d\n", *p); } Zhan Storage structure heap allocation theater payees
Regain the dynamic memory allocation of the C language