Week 3 - Pointers and Memory Addressing Flashcards

1
Q

Name the 5 base data types in C.

A

Char, int, float, double, pointer

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

How would you declare a pointer p to a character in C?

A

char p;
or
char
p;

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

How would you assign p to the address of/as a pointer to ch in C?

A

p = &ch;

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

char ch = ‘A’;
char *p;
p = &ch;
What does p contain?

A

The address of where ch is stored in memory

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

If x is any datatype, then what is &x in C?

A

The location in memory (its “address”) where x is currently stored

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

If x is a pointer in C, then what does <datatype> *x; declare?</datatype>

A

Declares x to contain the memory address of a variable of type <datatype></datatype>

*x says: go to the memory address stored in x and bring back the value (of type <datatype> stored there</datatype>

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

What is “dereferencing the pointer” in C?

A

*x, which says: go to the memory address stored in x and bring back the value (of type <datatype> stored there</datatype>

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

How do you calculate the amount of memory used to store a pointer?

A

sizeof(), normally dependant on the OS

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

What can void pointers do in C?

A

Point to any datatype, but need to be cast when used

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

What are the two runtime errors when using pointers in C, and how are they caused?

A

Memory fault & segmentation fault

Caused by trying to access memory which it is not allowed to access, and trying to access memory in a way that is not allowed, e.g. trying to overwrite the operating system

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

Sometimes in C, you don’t know at compile time how much memory you will need at runtime. What are the 3 possible solutions?

A
  1. Declare an array whose size is decided at runtime (but no way of knowing how much free contiguous memory is available or how much is required before declaring the array)
  2. Process the data in smaller chunks (possible with lots of extra programming)
  3. Dynamically allocate memory at runtime using pointers (so can check whether memory allocation was successful)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What C header file is needed to include NULL and malloc?

A

stdlib.h

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

When using malloc in C, what must you always trap?

A

The fact that memory was not allocated

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

When using malloc in C, when must you always free the memory?

A

Before the variable goes out of scope

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

How do you free memory stored in pointer p in C?

A

free(p);

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

Why should you always free memory in C?

A

Freeing memory returns it to the system, allowing it to be re-used

17
Q

In C, if a pointer p is freed, what does p become?

A

A dangling pointer

18
Q

In C, what can you use to test that all memory is freed properly?

A

The valgrind utility

19
Q

In C, what does malloc do?

A

Allocate memory to a variable at runtime, e.g.

int *p = NULL;
p = (int *)malloc(23)

^ this allocates 23 bytes!