C++: Reference to dynamic memory


All considerations about when to use which aside, I am still unsure about pointer vs reference semantics.

Right now, I am under the impression that references are essentially pointers that must be initialized when they are declared, and then from that point on cannot point to anything else. In other words, they are a like a Type* const (not Type const*), or, they cannot be reseated. It essentially becomes a "new name" for that object. Now I heard that references do not actually need to be implemented by the compiler using pointers, but I am under the impression that you can still think of them this way, in regards to what their visible behavior will be.

But why can't you do something like this:

int& foo = new int;

I want to create a reference to dynamic memory. This does not compile. I get the error

error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int*'

That makes sense to me. It seems the new operator returns a pointer of given type to the address of memory that the OS? dynamically allocated for me.

So how do I create a "reference" to dynamic memory?

Edit: Links to resources that precisely explain the difference between references and pointers in C++ would be appreciated.



new returns a pointer to the allocated memory, So you need to capture the return value in a pointer.

You can create a reference to a pointer after allocation is done.

int *ptr = new int;
int* &ref = ptr;

then delete it after use as:

delete ref;

or more simply,

int &ref = *(new int);

delete it after use as:

delete &ref;

References are syntactic sugar. They allow one to access an object with the dot operator rather than the arrow.

Your choice of whether to use a pointer or a reference is semantic. When you pass an object by reference to a method, or return a reference from a method, you are saying: "This is my object and you may use it, but I own it (and it may be on the stack or the heap.)" It follows that the other answers here which suggest syntax like delete &foo; might technically work, but smell bad; If you have a reference to an object then you shouldn't be deleting it. You don't own it and, most importantly, as you can't reset the reference you end up with a reference to deallocated memory, which is a bad thing.

Now, if you have allocated an object on the heap (called 'new' to create it) then you do own it, and are responsible for cleaning it up later, so you need to hold a pointer to it. Why? So you can safely delete it later and null-out the pointer.

It follows that the difference between a pointer and a reference, other than the mechanical difference of using dot rather than arrow, is that by passing by reference to a method you indicate something about how an object should be used. To initialise a reference directly by calling new is nonsense, even if possible.


You can get a reference like this:

int& foo = *(new int);

In general, to get from T* to T& you use * to "dereference" the pointer.

However this is not a very good idea in the first place. You usually use pointers to store addresses of heap-allocated objects.