Week 3: Pointers Flashcards

1
Q

When a variable is declared, what determines how much memory is assigned to the variable?

A

The type of the variable determines the amount of memory assigned to it. For example, an integer is 4 bytes, thus would be allocated 4 contiguous memory cells. [ 2^(4*8) ]

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

What type is a pointer?

A

A pointer is its own data type that is unique to the type it points to. For example, a pointer to an integer is a type pointer that points to an integer.

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

Give an example of how you would use a triple pointer? (Code an example if possible)

A

p points to x, q points to p, z points to q. Triple dereference q would give the value of x.

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

What does the variable of an array point to?

A

The memory address of the first element of the array.

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

What does a pointer to an array point to?

A

The memory address of the first element of an array.

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

If pointing to the beginning of an int array, in regards to output, what happens if the pointer is incremented by 4?

A

The pointer moves 4 indexes up the array, meaning each pointer incrementation moves 4 bytes along the array. In general, when a pointer is incremented, it will increment that same number of bytes of the data type it’s pointing to.

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

When does malloc assign the memory?

A

At run time.

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

Why are void pointers useful in memory allocation?

A

When we are not sure which data type we will be mallocing, thus using a void pointer means we can cast at a later time.

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

What are the 3 methods of dynamically allocating memory? What are the issues with 2 of them? Why is the 3rd method best practice?

A
  1. Declare an array whose size is decided at run time -> We don’t know how much free memory is available, or how much we need.
  2. Process the data into smaller chunks -> Very complex.
    3 Use pointers (Malloc) -> Can check whether memory allocation was successful.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe how malloc works…

A

Malloc asks the OS for X amount of contiguous bytes in memory from the heap. X depends on the data type of the malloc. The OS will assign the memory providing there is contiguous memory available.

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

When calling malloc, what happens if the requested memory available is fragmented?

A

Malloc will not assign the memory. Malloc will only ever return contiguous memory.

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

What does malloc return?

A

A void pointer to the first address of the assigned contiguous memory.

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

Why should we cast what malloc returns?

A

To cast the returned void pointer to the correct data type.

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

How do we know if malloc has failed?

A

It returns nil.

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

What are the 2 runtime errors that pointers can cause? What causes them?

A

Memory fault and segmentation fault.
1 - Caused by pointers trying to access memory it’s doesn’t have rights to.
2- Caused by pointers trying to access memory in an illegal way i.e overwrite OS files.

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