CS136 FINAL Flashcards
What are the advantages of HEAP
Dynamic - size of memory determined at runtime
Resizable - allocated memory can be “resized”
Scope - allocated memory “persists” until you free it; memory is valid even with function returns
Safety - if memory runs out, it can be detected and handled properly.
Which library is malloc declared in?
What is the run time of Malloc
<stdlib.h>
runtime: O(1)
</stdlib.h>
What does <stdlib.h> provide in general</stdlib.h>
anything memory allocation related.
EX: malloc, realloc, calloc, free, bsearch, qsort
What does free do?
What is the runtime of free?
What happens when u print ptr again?
deallocates space that has been allocated. does nothing if ptr is NULL
invalidates pointer
runtime: O(1)
throws an error: heap-use-after-free
What is a dangling pointer? What should we do to prevent this and why should we do it?
A pointer to a freed allocation.
Should assign NULL to a dangling pointer, this way there is no error when you free that pointer again.
How does a memory leak occur with heap memory?
When allocated heap memory is not freed before program termination.
what are the side effects of heap ?
it modifies the state of heap,
if memory is not freed within the func that allocates it then we need to document that
“caller must free”
if it is freeing memory that it didnt allocate
“data becomes invalid”
Describe merge_sort
see answers on phone