1 KiB
id | title | tags | created | ||
---|---|---|---|---|---|
18bl | Heap_memory |
|
Saturday, April 20, 2024 |
Heap memory
Along with Stack_memory, programs make use of heap memory during runtime.
Heap memory does not use a standardised data structure and can be accessed from any point within the program.
Whereas stack memory with it's LIFO structure has memory management built-in when programs allocate memory from the heap they must manually deallocate it when it is no longer required. This process of "freeing memory" is known as garbage collection. In a language like C, this is the explicit concern of the programmer and is not abstracted away. Failure to properly manage garbage collection is what causes Memory_leaks.
Here is an example of managing heap memory allocation in C:
void * data;
data = malloc(512)
The first line assigns a special pointer variable (indicated by void *
rather than int
or str
) . This is a variable only holds a memory address.
The malloc
method assigns 512 bytes to the data
variable.