Week 7 Flashcards

1
Q

What do we use to reference memory in the heap?

A

Pointers

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

Who assigns memory?

A

The operating system

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

The OS gets memory back when the program ends, but what is good programming practice?

A

To return it yourself

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

What do we call getting memory from the heap in C?

A

memory management.

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

What do we need to include for memory management?

A

stdlib.h

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

How do we determine the number of bytes to request?

A

sizeof() function

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

C lets you use a pointer as if it was an array. How do we access an element beyond where the pointer is pointing?

A

Using the […] notation to access elements beyonce where the pointer is pointing.

a_pointer[5] = 20;

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

A common error with data structures and malloc is that the caller doesn’t allocate the space for the top structure. How do we write this properly?

A
bool stack_initialize(stack_t*the_stack){}
main() {
stack_t the_stack;
stack_initialize(&the_stack);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A solution to the problem of a caller not allocating space is to do what?

A

Make the data structure responsible for all its space

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

What is an alternative to malloc?

A

calloc

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

Once allocated, does space in the heap change size?

A

Only if you ask it to change size

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

After using realloc, any data in the previous space is copied into a new space. Are previous pointers into the old space still valid?

A

No, they’re no longer valid. You’re not guaranteed to get back a pointer to the same pace as before.

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

How do we return space to the heap with a function?

A

void free(void *memory_to_return);

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

What are the two main options for realloc?

A

Grow in place if possible

Copy to a new location if necessary

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

Free does not clean up your pointer. How do we avoid mistakes?

A

Don’t keep using the pointer. You could be accessing memory that is given to another data structure!

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

What do we do when we want dynamic arrays?

A

Use malloc or calloc

Track how full the array is

Use realloc to grow the array size when the array gets full

17
Q

What is a common trick to increasing the size of an array?

A

Start the array small and double its size each time that it needs to grow. At most half the array is empty after the first realloc

18
Q

What is a linked list?

A

A sequence of data values that are put together in a chain.

19
Q

What is a node?

A

One data value in the list

20
Q

Where does the last node in a linked list point?

A

null

21
Q

What do we include in the data type for a linked list node?

A

We use a struct to include both a value (an int or string) and a pointer

22
Q

What does the pointer in a node point to?

A

Another linked list node of the same data type

23
Q

how do we declare the pointer to a data type that we’re currently declaring?

A

Give the struct itself a name and use that name along with the struct keyword.