Lecture 3 - Memory & Pointers Flashcards

1
Q

What is memory?

A

Part of the computer which is used to store data and program instructions for retrieval.

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

What can we think of memory as

A

a sorting cabinet, where each box stores the value and the label is the variable name

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

What notion do we have in memory?

A

A notion of spatial locality. This is an important property of which caches make use of. This is when a line of memory is fetched,so close by data that may be needed soon can be loaded quickly (is cached).

  • We are making a guess as to what data will be needed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How long are addresses in 64 bit architecture?

A

64 bits or 8 bytes

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

How big is a memory addressed by an address?

A

a byte (8 bits)

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

How many addresses can a 64 bit architecture have ?

A

2^64 addresses to bytes

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

Do we need to get the full byte to modify a single bit ?

A

YES

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

What doe (1 &laquo_space;3) mean ?

A

A left shift (multiplication by 2).

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

What does & do?

A

Gets the address of a variable -> pointer.

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

what is sizeof()

A

A function that gets the size of a variable in bytes

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

What is a pointer?

A

A variable which has the value , which is the address of a memory location. It essentially points to where the data is.

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

What is * called.

A

The dereference operator?

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

What does * do?

A

It gets the value at the address.

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

Is every pointer the same size?

A

YES

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

How big are pointers in 64 bit architecture?

A

8 bytes (64 bits)

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

Is pointer a normal variable.

A

Yes in a sense that is also has to be stored at some memory location.

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

What is int * * name?

A

A pointer to a pointer which points to data.

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

How are arguments passed to a function in C?

A

By value?

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

Are the any exceptions to call by value?

A

Yes, arrays. Pointers are also copied by the value they point to can be manipulated directly.

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

Are int param[] and int * param interchangeable?

A

YES

21
Q

What do we use to represent that a pointer points to nothing.

A

NULL or 0

22
Q

What will happen if we try to dereference a NULL pointer?

A

The program will crash.

23
Q

What is const?

A

a type qualifier, which indicates the content of the variable cannot be changed. This is enforced by the compiler.

24
Q

What are the 3 ways that a pointer is unmodifiable.

A
  • pointer itself cannot be changed via float * const ptr;
  • the value we are pointing to cannot be changed via const float * ptr;
  • both via const float * const ptr;
25
Q

What does the name of the array refer to ?

A

The memory location at which the first element of the array is stored.

26
Q

Are ptr[i] and *(ptr+i) equivalent

A

YES

27
Q

Can an array be changed?

A

No, but its values can.

28
Q

What can pointer arithmetic be used for?

A

Modifying value of the pointer.

29
Q

What can we do via pointer arithmetic?

A

Subtract/Add from/to pointer
Subtract 2 pointers from each other
compare pointers

30
Q

Does pointer arithmetic take into account the size of the type of data it is storing?

A

YES, it does so automatically.

31
Q

What is ptr->hello?

A

it is equivalent to (*ptr).hello. It is the notation for accessing a member of a struct.

32
Q

What does main look like when we want to process command line arguments?

A

int main(int argc , char * argv[])

33
Q

What is argc

A

argc specifies the number of command line arguments that have been written in the command

34
Q

What is argv?

A

The actual command line arguments as strings.

35
Q

What is the alternative way of writing char * []?

A

char * *

36
Q

What is void * used for?

A

Writing generic code that works with all data types.

37
Q

Can all pointers be converted to void *?

A

Yes

38
Q

When would void * prove to be useful?

A

When swapping variables of differnt types, this will simply swap the bytes, where the bytes have no meaning

39
Q

Can we get the value pointed to by void *?

A

No , the pointer type has to be changed first.

40
Q

What variables are stored on the stack?

A

Automatic variables, the size of these are known statically

41
Q

What is stored on the heap?

A

Dynamically allocated variables , where we have to manage allocation and freeing of the memory

42
Q

Do heap and stack share the same memory space?

A

Yes , stack goes top to bottom, while heap bottom to top. They grow towards each other as they fill up.

43
Q

How can we request a new chunk of memory from the heap?

A

using malloc with number of bits we need, which returns a void pointer, so remember to convert

44
Q

What is returned if malloc fails?

A

NULL

45
Q

What command is used to free memory?

A

free

46
Q

What happens if free is not called?

A

We leak memory, so that it is wasted.

47
Q

What is good to do after freeing memory?

A

Setting the pointer to it to NULL.

48
Q

What should we never return from a function?

A

A pointer to a function local variable, this is because it’s lifetime will end at the end of the block.

We can deal with this by allocating memory and passing the pointer back. Or manipulating it in the function in relation to e.g. root node