References and Pointers Flashcards
give existing variables new names
allow modification of arguments or items
avoid copying data
references
the original iterator
pointers
int x = 5;
int y = x;
what is the value of y?
5
int x = 5;
int y = x;
y = 10;
what is the value of x now?
5
the line y = x; copied the value of x into y
the blank makes a reference
& ampersand;
a reference is a blank
new name for something
True or False
you can change what a reference refers to after it is created
False
One of the main places we use references is with blank
function arguments
why is the argument for print_vector both const and a reference?
a const reference argument avoids the cost of copying the vector, but does not allow the vector to be accidentally modified by the function
blank is usually the right way to pass an argument
const reference
unless
the function needs to modify the variable passed in
the argument has a simple data type, such as int or double
References can be used with blank to allow modification to the value of the items in the container:
range-style for loops
references and range-style for loops
for(int&x : numbers)
{
x = x * 2;
}
for (pair& p : scores)
{
p.second = p.second + 5.0;
}
**no need for iterators
blank is like a big vector of bytes
memory
each byte has an address; the first byte is blank, the second is blank, etc
first byte is 0
the second byte is 1
Each object is given a blank of those bytes to store its value
subsequence
ex: an int is given 4 bytes
a double is given 8 bytes
the address of an object is the address of its blank
first byte
blank is a variable that holds the address of an object in memory
pointer
blank can refer to another variable
pointer
to declare a pointer, use blank after the type
*
int x = 5;
int* y;
y = &x;
what does that do
int x = 5 is a regular integer
int* y; pointer to an integer
y = &x make y point at x
when you are done with the ptr data, be sure to release it with blank
delete
delete x;
pointers can be made to point at entirely new data using the blank keyword
new
data allocated by new is allocated in an area called blank
free store
containers like string, vector, and map place their contents in the blank
free store
most variables are allocated at the end of memory in an area called the blank
call stack
data in the free store remains allocated until we use the blank key word
delete key word
blank allow you to access and modify another variable
pointers and references
require special syntax
pointers
such as * deference operator
such as & (address-of operator)
can be moved to point at a new variable
pointers
can reference to entirely new data
pointers
also available in pure C
pointers
no special syntax
references
cannot change which variable is referred to
references
cannot refer to entirely new data
references
new to C++
references