C++: Pointers, References, and Memory Allocation
NOTE: You should first read the first parts of this page to get the fundamentals of pointers, then read this page.
C++ Pointers
C++ pointers are the same as C pointers, other than the way we allocate memory for new variables to point to (see below). So we don’t use malloc() and free(), but otherwise they work the same.
You can have pointers of any type, and one very important kind is a pointer to a class type. This creates a variable that can point to an object, and we use this capability a lot.
C++ pointers to objects behave essentially the same as Java class variables. In Java, all variables declared of class types are references – meaning, they do not directly hold an object, but rather hold a reference, or pointer, to an object. In Java, all objects are created using new. In C++, we will most often create objects using new, but you can create objects by just declaring an object variable.
C++ has two different member access operators: dot .
is used
on actual objects, while arrow ->
is used on pointers to objects.
Arrow is itself a dereferencing operator, and so you do not need to
add a *
in front of the pointer for dereferencing.
References
Serious programming in plain C uses a lot of pointers, and pointers are
often dangerous and error-prone. C++ introduced references as a way to
not rely as heavily on pointers. A reference variable is declared with an
&
on the type. Unlike Java references (all Java class variables are
references), C++ references do not change. Java references are like
C++ pointers, but just more type and memory safe. C++ references are
aliases, or another name for some object. You cannot change what a
C++ reference refers to.
In other words, once you initialize a reference to refer to something, it cannot change what it refers to. And you must initialize it.
There are really only two places where references are useful: as explicit pass by reference parameters, and as member data attributes in an object.
We can create a function, say int f(int& x)
, that declares a simple
integer variable as pass by reference; this can sometimes be useful if an
“extra” return value is needed, and it can be passed back by modifying the
parameter.
But most often, we use explicit reference parameters for objects. Objects typically have quite a few data fields and so are “big”; copying these as pass-by-value parameters wastes time and space and energy, so it is better to pass objects by reference.
For example, a method such as bool validate(Customer& customer)
would
use pass-by-reference on the customer object, rather than copying it. Of
course, this means the method could modify the object; if we want to
protect the object, we would declare the method as
bool validate(const Customer& customer)
, which says that the customer
object is constant, meaning it is not allowed to be modified.
The other useful place for references in inside an object as a data field. Suppose our Customer has an Address object associated with it; we could declare a pointer data field, but better would be a reference data field:
class Customer {
public:
Customer(Address& address);
private:
Address& address;
}
NOTE: assume there is a lot of other stuff in the Customer class, we just are focusing on the address.
By using a reference, all of our code in the Customer class can use the dot (.) notation rather than an arrow, and it all looks cleaner. Plus, the compiler can do more error checking.
But, a reference must be initialized, and then cannot change. To do this, we must use the initializer syntax on our constructor:
Customer::Customer(Address& address)
: address(address)
{
// other initialization in the constructor body
}
Memory allocation and deallocation (C++)
Plain C programming uses the library functions malloc()
and free()
to
allocate and deallocate memory, but these functions are simplistic and
not safe – they do not know anything about the types of your variables
and data.
So, C++ introduces built-in operators new and delete as better, safer alternatives to the plain C functions.
Other resources
http://cplus.about.com/library/weekly/aa040702a.htm About.com’s Pointer help page
http://cplus.about.com/library/weekly/aa040802a.htm About.com’s Array help page
http://cplus.about.com/library/weekly/aa042402a.htm About.com’s Memory Allocation help page