Lecture 3 - Memory & Pointers Flashcards
What is memory?
Part of the computer which is used to store data and program instructions for retrieval.
What can we think of memory as
a sorting cabinet, where each box stores the value and the label is the variable name
What notion do we have in memory?
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 long are addresses in 64 bit architecture?
64 bits or 8 bytes
How big is a memory addressed by an address?
a byte (8 bits)
How many addresses can a 64 bit architecture have ?
2^64 addresses to bytes
Do we need to get the full byte to modify a single bit ?
YES
What doe (1 «_space;3) mean ?
A left shift (multiplication by 2).
What does & do?
Gets the address of a variable -> pointer.
what is sizeof()
A function that gets the size of a variable in bytes
What is a pointer?
A variable which has the value , which is the address of a memory location. It essentially points to where the data is.
What is * called.
The dereference operator?
What does * do?
It gets the value at the address.
Is every pointer the same size?
YES
How big are pointers in 64 bit architecture?
8 bytes (64 bits)
Is pointer a normal variable.
Yes in a sense that is also has to be stored at some memory location.
What is int * * name?
A pointer to a pointer which points to data.
How are arguments passed to a function in C?
By value?
Are the any exceptions to call by value?
Yes, arrays. Pointers are also copied by the value they point to can be manipulated directly.
Are int param[] and int * param interchangeable?
YES
What do we use to represent that a pointer points to nothing.
NULL or 0
What will happen if we try to dereference a NULL pointer?
The program will crash.
What is const?
a type qualifier, which indicates the content of the variable cannot be changed. This is enforced by the compiler.
What are the 3 ways that a pointer is unmodifiable.
- 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;
What does the name of the array refer to ?
The memory location at which the first element of the array is stored.
Are ptr[i] and *(ptr+i) equivalent
YES
Can an array be changed?
No, but its values can.
What can pointer arithmetic be used for?
Modifying value of the pointer.
What can we do via pointer arithmetic?
Subtract/Add from/to pointer
Subtract 2 pointers from each other
compare pointers
Does pointer arithmetic take into account the size of the type of data it is storing?
YES, it does so automatically.
What is ptr->hello?
it is equivalent to (*ptr).hello. It is the notation for accessing a member of a struct.
What does main look like when we want to process command line arguments?
int main(int argc , char * argv[])
What is argc
argc specifies the number of command line arguments that have been written in the command
What is argv?
The actual command line arguments as strings.
What is the alternative way of writing char * []?
char * *
What is void * used for?
Writing generic code that works with all data types.
Can all pointers be converted to void *?
Yes
When would void * prove to be useful?
When swapping variables of differnt types, this will simply swap the bytes, where the bytes have no meaning
Can we get the value pointed to by void *?
No , the pointer type has to be changed first.
What variables are stored on the stack?
Automatic variables, the size of these are known statically
What is stored on the heap?
Dynamically allocated variables , where we have to manage allocation and freeing of the memory
Do heap and stack share the same memory space?
Yes , stack goes top to bottom, while heap bottom to top. They grow towards each other as they fill up.
How can we request a new chunk of memory from the heap?
using malloc with number of bits we need, which returns a void pointer, so remember to convert
What is returned if malloc fails?
NULL
What command is used to free memory?
free
What happens if free is not called?
We leak memory, so that it is wasted.
What is good to do after freeing memory?
Setting the pointer to it to NULL.
What should we never return from a function?
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