SYSC 2006 Lecture Notes - Lecture 10: C Dynamic Memory Allocation, Operator Overloading, In C
Document Summary
Heap data structure organizes data through heap_sort algorithm (very clean and efficient) In c, we have the choice of using global memory(cautioned to use as little as possible), parameters and variables, and a region of stored data called heap. A variable cannot be declared in the heap. Data for variables is accessed through pointers in the heap. Parameters and local variables of a function have a lifetime limited to the lifetime of the function. Heap memory is different, we have complete control over lifetime of heap memory. Memory can be allocated to one function and deallocate several functions later. Lifetime of heap memory is independent of the duration of the function frame. Allocate a block of memory big enough to hold one value type of int. #include int *p; //variable name is p, type is pointer to int p = malloc(sizeof(int)); if (p != null) {