Dynamic Memory Allocation Flashcards

1
Q

Why are variable sized arrays problematic in C?

A

Not all versions of C support them

They are stored in the stack which has a limited amount of space

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

Where are local variables and function parameters stored?

A

On the stack

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

What is the heap used for?

A

Dynamic memory allocation at run time

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

What argument does malloc take?

A

The number of bytes to reserve on the heap

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

What does malloc return?

A

It returns a pointer to the memory space it has reserved

It returns a pointer of type void*

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

Who is responsible for freeing memory that has been reserved using malloc?

A

The programmer

This is done using the free(pointer)

function

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

Where are global variables stored?

A

In Static Data

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

How is memory in the heap accessed?

A

It can only be accessed through pointers.

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

How would we create a 2d int array that is of size AxB?

A
  1. Create a pointer to an array of pointers.

int** 2d_arr = malloc(A * sizeof( int* ));

  1. Assign a pointer to an int array to each element in the pointer array

for all pointers in the pointer array:

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

What are two important things to remember when using malloc?

A
  1. Always check that malloc hasn’t returned -1, indicating a failure
  2. Remember to free the memory after it has been used, using the free(pointer) function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly