Dynamic Memory Allocation Flashcards

1
Q

What are two functions used for memory allocation in C?

A

malloc() and calloc()

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

What does malloc() do?

A

Allocates a given number of bytes

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

What does calloc() do?

A

Allocates memory for a given number of elements, where each element is of a specified number of bytes. Also clears memory (every byte initialized to zero)

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

What do malloc() and calloc() both do?

A

Return a pointer to void

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

What is ‘void’?

A

A placeholder data type

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

What does void indicate?

A

Either no data type, or any data type

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

What is typecasting?

A

Explicit data type conversion

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

What is typecasting commonly used for?

A

Pointers, to change the data type that a pointer points to. This is possible because all pointers are the same size.

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

What are the two ways to access contiguous elements?

A

Using pointer notation and pointer arithmetic, or using array notation (subscripts)

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

Why deallocate memory?

A

Otherwise we get a memory leak

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

Which function deallocates memory at runtime?

A

Free()

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

What is a memory leak?

A

Block of dynamically allocated memory with no pointers to it

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

How do memory leaks happen?

A

A pointer gets either clobbered or it moves out of scope

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

How do we prevent memory leaks?

A

Always explicitly deallocate memory when you’re done with it, if a function allocates memory then pass the pointer by reference

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

What is a double pointer?

A

A pointer variable that contains the address of another pointer variable

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

Why are double pointers needed?

A

To pass pointers by reference, and for dynamically allocated arrays of pointers