Since malloc is allocated dynamically, why should we specify size?

  c++, question

In the insert () function in the following code, cnu++ can understand why len should have len++?
If so, wouldn’t it be possible to insert array elements indefinitely? So what’s the point of judging whether full?
In addition, when I run according to the following code, I allocated 6 spaces when initializing the array, and I can insert the 7th and 8th elements, so where do these elements store the space? Did malloc allocate it automatically? If I add an element malloc to automatically allocate space, why should I specify size when using malloc?

#include<stdio.h>
 #include<malloc.h>
 #include<stdbool.h>
 struct Arr{
 int len;  //The maximum number of elements the array can access
 int cnu;  //The current number of elements in the array
 int * pBase;  //Store pointers to arrays
 };
 
 /**
 * Initialize array
 */
 void init_array(struct Arr * pArray,int len){
 pArray->pBase=(int *)malloc(sizeof(int)*len);  //Allocate 4*len bytes of memory
 If(NULL== pArray->pBase)// determines if memory allocation failed
 {
 Printf ("Dynamic Memory Allocation Failure \n");
 // exit(-1);
 }else{
 pArray->len=len;
 pArray->cnu=0;
 }
 return ;
 }
 
 /**
 * Judge whether the array is empty, transfer address to save 4 bytes of memory, transfer structure variables need to be copied, 12 bytes
 */
 bool isempty(struct Arr * pArray){
 if(0==pArray->cnu)
 {
 return true;
 }else{
 return false;
 }
 }
 
 /**
 * * Determine if the array is full
 */
 bool isfull(struct Arr * pArray)
 {
 if(pArray->len==pArray->cnu)
 {
 
 return true;
 }else {
 return false;
 }
 
 }
 /**
 * Display array contents
 */
 void show_array(struct Arr * pArray){
 if(isempty(pArray))
 Printf ("array is empty!  \n");
 else{
 int i;
 for( i=0;   i<pArray->cnu;  i++)
 {
 printf("%d \n",pArray->pBase[i]);
 }
 printf("------------------------------------\n");
 }
 }
 
 /**
 * * Appends elements to array
 */
 bool append(struct Arr * pArray,int val){
 if(isfull(pArray))
 {
 Printf ("array is full!  \n");
 return false;
 }else{
 pArray->pBase[pArray->cnu]=val;
 pArray->cnu++;
 
 }
 }
 
 /**
 * * insert elements into the array, pos is the position in the array, pos=3 is to insert elements into a[2]
 */
 
 bool insert(struct Arr * pArray,int pos,int val)
 {
 If(pos<1||pos>pArray->len+1)// cannot be inserted at a position less than 1, and cannot be two years larger than the last element
 {
 Printf ("Illegal input of inserted position \n");
 return false;
 }
 if(isfull(pArray))
 {
 Printf ("Array is full, insert failed!  \n");
 return false;
 }
 int i;
 //loop moves back the array where pos position starts
 for(i=pArray->cnu;  i>=pos;  i--)
 //The moving range is from pos to cnu
 {
 pArray->pBase[i]=pArray->pBase[i-1];
 /**
 If I indicates the position of the element to be moved, from the beginning.  On the right are i-1. If you move left, i-2 is on the left, I move right, and I is on the left.
 */
 }
 
 pArray->pBase[pos-1]=val;
 pArray->cnu++;
 pArray->len++;
 return true;
 }
 /**
 * * Deletes pos element number one in the array and returns the value of the deleted element
 */
 
 bool delete(struct Arr * pArray,int pos,int * val)
 {
 if(pos<1||pos>pArray->cnu)
 {
 Printf ("Delete Failed, Location Illegal \n");
 return false;
 }
 int i;
 *val=pArray->pBase[pos-1];
 for(i=pos+1;  i<=pArray->cnu;  i++)
 {
 //The mobile unit is from pos+1 to cnu
 pArray->pBase[i-2]=pArray->pBase[i-1];
 
 }
 pArray->cnu--;
 return true;
 
 }
 
 /**
 * * array inversion
 */
 
 bool inverse(struct Arr * pArray)
 {
 if(isempty(pArray))
 {
 Printf ("Inversion failed because array is empty");
 return false;
 }
 
 int i=0;
 int j=pArray->cnu-1;
 
 int temp;
 while(i<j)
 {
 temp=pArray->pBase[i];
 pArray->pBase[i]= pArray->pBase[j];
 pArray->pBase[j]=temp;
 i++;
 j--;
 }
 return true;
 
 }
 int main()
 {
 struct Arr arr;
 init_array(&arr,6);  //Take the address of the structure as an argument so that the value in the structure can be modified. If the structure variable is passed, it will be copied without changing the value
 append(&arr,1);
 append(&arr,2);
 append(&arr,3);
 append(&arr,4);
 show_array(&arr);
 insert(&arr,2,88);
 show_array(&arr);
 int val;
 delete(&arr,1,&val);
 show_array(&arr);
 Printf ("deleted %d\n",val);
 inverse(&arr);
 show_array(&arr);
 return 0;
 }
  1. len++It is the wrong code

  2. mallocJust apply to the system for a fixed amount of memory and return the starting address to you. It will not be adjusted automatically. The 7th and 8th elements have overflowed and will cover the two memory cells after these 6 spaces. If these two memory cells hold other data, unknown errors will be caused.

  3. Can 8 elements be inserted or becauselen++This bug, every insert will makelen++By comparisonlenAndcnuThe array will neverfull