Does referencing solves the pointer problems?

42 views Asked by At

Hi I'm very confused using references. If we use reference instead of pointer does it solve the dangling or even memory leakage problem?

1

There are 1 answers

0
Chad On

(Thinking specifically of C/C++ here): a reference is just an alias, or another name for an existing object. It is usually a lot harder to create a null reference than a null pointer, so it can help solve the "dangling" problem.

Since a reference is assigned to an existing variable, it can't really contribute to a memory leak. But it is possible to have a reference to memory that has been deallocated.