Abstraction of memory access Flashcards
What is a variable in terms of memory?
A method of storing information in your computer’s memory.
Define declaration:
Tells the compiler the name and type of something.
Define definition:
In addition to the declaration, it tells the compiler where it is stored in memory.
In C how would we find the the variable int x = 10; address?
&x (ampersand operator)
Why do we want a variable’s address?
C only allows pass by value and return by value.
What is pass by value?
When passing a variable into a function, a copy is always made.
How does the ampersand (&) operator overcome pass by value?
It allows us to pass the memory address of where data is stored instead.
What are pointer variables?
Variables that store memory addresses as their value (unsigned integers).
How do we refer to pointers in C?
By the data type of the thing they point to followed by an asterisk (*)
e.g. int * is a int pointer
How would you deference a pointer to a pointer?
Adding two asterisks to the pointer variable:
int i;
int *ptr_to_i = &i;
int **ptr_to_ptr_to_i = &ptr_to_i;
// i == **ptr_to_ptr_to_i;
What are arrays in C?
A block of continuous memory.
How do we keep track of the length of an array?
There is no .length() therefore we must manually keep track.
How are strings stored in C?
There is no string type in C, therefore contiguous blocks of chars in memory terminated by a null character (/0) is used.
How do we refer to a string in C?
The location of the first char in memory.
Is passing pointers the same as pass by reference?
No, because C just creates a copy of the pointer.