What are some useful examples of malloc() in C?

前端 未结 6 1462
傲寒
傲寒 2020-12-15 08:16

I\'m just reading about malloc() in C.

The Wikipedia article provides an example, however it justs allocate enough memory for an array of 10 ints in com

6条回答
  •  悲哀的现实
    2020-12-15 08:50

    I recommend that you google Stack and Heap.

    int* heapArray = (int*)malloc(10 * sizeof(int));
    int stackArray[10];
    

    Both are very similar in the way you access the data. They are very different in the way that the data is stored behind the scenes. The heapArray is allocated on the heap and is only deallocted when the application dies, or when free(heapArray) is called. The stackArray is allocated on the stack and is deallocated when the stack unwinds.

提交回复
热议问题