CS136 FINAL Flashcards

1
Q

What are the advantages of HEAP

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which library is malloc declared in?
What is the run time of Malloc

A

<stdlib.h>
runtime: O(1)
</stdlib.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does <stdlib.h> provide in general</stdlib.h>

A

anything memory allocation related.
EX: malloc, realloc, calloc, free, bsearch, qsort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does free do?
What is the runtime of free?
What happens when u print ptr again?

A

deallocates space that has been allocated. does nothing if ptr is NULL
invalidates pointer

runtime: O(1)

throws an error: heap-use-after-free

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a dangling pointer? What should we do to prevent this and why should we do it?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does a memory leak occur with heap memory?

A

When allocated heap memory is not freed before program termination.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what are the side effects of heap ?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe merge_sort

A

see answers on phone

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly