OK. I give up! Where DID you put my
variables???? |
Memory Addresses
During program execution, each object (such as a variable or an array) is located
somewhere in an area of memory.
The location of an object in the memory is called its address. In C++ there is
an address (or referencing ) operator, &, which allows you to obtain an object's address.
The value returned by this address operator indicates the location of an object in memory.
Memory locations are determined by the operating system and it is
possible that objects' addresses may be different during repeated runs of
a program.
|
C++ offers two mechanisms for dealing with
addresses: references (&) and pointers(*).
Pointers are an older mechanism that C++ inherited from C, while references are a newer mechanism that originated in C++.
Unfortunately, many
C++ library functions use pointers as arguments and return values, making it
necessary for programmers to understand both mechanisms.
Definition: A pointer
is a variable that holds a memory address. |
If you know the address of an object, it is not difficult to find out what is stored at that address. There is a special operator in C++,
* , called the
de-reference operator. Even though
this symbol is the same as the multiplication symbol, the compiler will determine from the context, which of these two operators it should use.
If there is an address of an object to the right of the * operator,
such as,
* address
then the result of the operation will be the value of the object stored at this address.
|