Heap Memory in C Programming

后端 未结 4 1554
抹茶落季
抹茶落季 2020-12-18 10:25

What exactly is heap memory?

Whenever a call to malloc is made, memory is assigned from something called as heap. Where exactly is heap. I know that a program in mai

4条回答
  •  心在旅途
    2020-12-18 11:16

    The heap is the diametrical opposite of the stack. The heap is a large pool of memory that can be used dynamically – it is also known as the “free store”. This is memory that is not automatically managed – you have to explicitly allocate (using functions such as malloc), and deallocate (e.g. free) the memory. Failure to free the memory when you are finished with it will result in what is known as a memory leak – memory that is still “being used”, and not available to other processes. Unlike the stack, there are generally no restrictions on the size of the heap (or the variables it creates), other than the physical size of memory in the machine. Variables created on the heap are accessible anywhere in the program.

    Oh, and heap memory requires you to use pointers.

    A summary of the heap:

    • the heap is managed by the programmer, the ability to modify it is somewhat boundless
    • in C, variables are allocated and freed using functions like malloc() and free()
    • the heap is large, and is usually limited by the physical memory available
    • the heap requires pointers to access it

    credit to craftofcoding

提交回复
热议问题