&
, to a variable to get
its location.
banana
is a variable, &banana
is its address.
// address.cpp -- using the & operator to find addresses #include <iostream> using namespace std; int main() { int donuts = 6; double cups = 4.5; cout << "donuts value = " << donuts; cout << " and donuts address = " << &donuts << "\n"; cout << "cups value = " << cups; cout << " and cups address = " << &cups << "\n"; return 0; } OUTPUT: donuts value = 6 and donuts address = 006AFDF4 cups value = 4.5 and cups address = 006AFDEC
*
operator to the pointer name, we get the value stored
at that address.
int Tomato = 7; // a variable Tomato set to 7 int* pTomato; // declare a pointer "pTomato" pTomato = &Tomato // assign the address of Tomato to the pointer *pTomato = 8 // By applying *, we change the value of Tomato to 8. // (i.e. Tomato is now 8)
*
operator is called the dereferencing operator.
typeName * pointerName;
int* pointerX
pointerX
is pointer-to-int
, or more
concisely, int *
.
&
operator
to a variable name to get an address of named memory.
new
operator to return the address of unnamed
memory.
*
) to it.
int * pi; pi = 0xBB000000; // type mismatchC lets you make assignments like this, but C++ more stringently enforces type agreement.
int * pi; pi = (int *) 0xB8000000; // types now match
typeName pointer_name = new typeName
int * pn = new int;
The new int
tells the program
you want some new
storage suitable for holding an int
. The
new
operator uses the type to figure out how many bytes are needed, then it
finds the memory and returns the address. Then, the address is assigned to
pn
.
delete
operator enables you to return memory to the memory pool when you
are finished with it. Memory that you return, or free, then can be reused by
other parts of your program.
new
with a use of delete
;
otherwise, you can wind up with a memory leak, that is, memory that has been
allocated but no longer ca be used.
delete
only to free memory allocated with new
. However, it
is safe to apply delete
to a null pointer.
new
, you can create an array during run time if you need it and
skip creating the array if you don't need it. Or, you can select an array size after
the program is running. This is called dynamic binding, meaning that the array
is created while the program is running.
type_name pointer_name = new type_name[num_elements];
int
s, do this: int* psome = new int[10]; // get a block of 10 ints
delete
that indicates you are freeing
an array: delete [] psome; // free a dynamic array
new
and delete
delete
to free memory that new
didn't allocate.
delete
to free the same block of memory twice in succession.
delete []
if you used new []
to allocate an array.
delete
(no brackets) if you used new
to allocate a single
entity.
delete
to the null pointer (nothing happens).
psome
points to the first element of the array, *psome
is the
value of the first element.
psome[0]
instead of *psome
for the first element,
psome[1]
for the second element, and so on.
wages
array, the statement
double * pw = wages;makes
pw
a pointer to type double
and then initializes pw
to wages
, which is the address of the first element of the
wages
array. So:
wages = &wages[0] = address of first element of array
arrayname[i] = *(arrayname + i)
pointername[i] = *(pointername + i)
int * pi = new int[10]; // pi points to block of 10 ints *pi = 5; // set zero element to 5 pi[0] = 6; // reset zero element to 6 pi[9] = 44; // set tenth element to 44 int tacos[10]; *(tacos + 4) = 12; // set tacos[4] to 12