What to do if the array space is not enough

What to do if the array space is not enough

Every time you define an array, you must determine its size. In case the space is not enough, what solution is there to solve it?

  1. Re-create a large array, and then move the original data one by one into the new array created — it would be too troublesome to do so, in order to add a new number, the time complexity of the operation to be done turned out to be O(n )
  2. At the beginning, the array is not built on the stack,
    int * a=(int * )malloc(3 * sizeof(* a));
    and then if the data is not enough, you can directly call the realloc() function
int  * temp=(int * )malloc(4*sizeof(*a))//还可以做一个判定操作,判定temp是否为空
free(a);//要释放之前创建的空间
temp =a ;   

This expands the original array to 4
3. Use a dynamic linked list

Guess you like

Origin blog.csdn.net/weixin_50994206/article/details/114995268