memory allocation Flashcards

1
Q

what is malloc used for

A

to get memory from the heap

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

what does malloc() return

A

a pointer to the start of the usable memory space (excluding meta data)

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

why do we use free() after using malloc()

A

the system can run out of space and there is no automatic garbage collection therefore we have to use free()

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

why should we not go outside the memory allocated via malloc()

A

because you will overwrite other data
usually the metadata for the rest of the allocatable space from malloc()

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

what are the four parts of the metadata in malloc()

A

whether the space is free
the length
prev and next pointers

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

what happens when we call free() (other spaces)

A

it coalesces all the free spaces around it

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

how does malloc create more efficient searching for different sizes of memory

A

similar size allocations are grouped together therefore you dont have to search the entire list
each section is usually in a different part of memory and is maintained with a doubly linked list

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

how is malloc usually implemented

A

a block of memory is added at the head of the free list like a stack
it maintains multiple free lists in different parts of memory
2 lists are used one being the actual block list and another that is explicitly free lists

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

what is the point of malloc using an actual memory block list and an explicit free memory block list

A

when scanning for space you dont have to look through the used blocks

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

what does each block tell us about the prev block and why

A

its length and whether its in use as it allows us to easily see whether we can coalesce it with the prev one
therefore when its updating its length and use status it also updates it in the next block

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

where is the used/free flag

A

in one bit from the length section

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

why do we have the length and used status as values rather than pointers

A

pointers require 8 bytes so this method requires less space

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

what happens when malloc runs out of memory

A

it calls sbrk(+/- value) which manages the top of the heap (that has a high water mark)

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

what do we call to read the high water mark of the heap

A

sbrk(0)

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

what happens when we call sbrk(positive value)

A

itll grow the heap and return the starting point of the extra memory

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

what happens when we call sbrk(negative value)

A

itll push down the heap thus passing memory back to the os

17
Q

a page

A

a 4k block of memory that the system manages memory in

18
Q

what do we use if we want multiple pages of memory

A

mmap(number of pages)

19
Q

map-anonymous

A

using mmap but not linking to a file

20
Q

unmap()

A

returns memory back to the os