Allocating A Large (5000+) Array

后端 未结 3 924
一向
一向 2020-12-06 07:34

I am working on an application were there are three possible sizes for the data entered:

  • small: 1000 elements
  • medium= 5000 elements
  • large= 50
3条回答
  •  北海茫月
    2020-12-06 07:47

    You can allocate such a big array on the heap:

    int *arr;
    arr = malloc (sizeof(int) * 500000);
    

    Don't forget to check that allocation succeded (if not - malloc returns NULL).

    And as pmg mentioned - since this array is not located in the stack, you have to free it once you finished working with it.

提交回复
热议问题