Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using references instead of pointers, resolve memory leaks in C++?

Most of memory leaks appear when a pointer of an object returned and programmer forgot to delete it.

for example:

class my_class
{
  ...
};

my_class* func1()
{
  my_class* c = new my_class;
  return c;
}

int main()
{
  my_class* var1 = func1();
  ...
  // Programmer forgot delete the var1: delete var1;
  // -- or --
  // Doesn't know 'delete[] var1;' is correct or 'delete var1;'.
}

Some of memory leaks appear when a pointer to an object created and programmer forgot to delete it.

for example:

class my_class
{
  ...
};

void func2(my_class* p)
{
  ...
}

int main()
{
  my_class* var3 = new my_class;

  func2(var3);

  // Does func2 deletes var3? Programmer doesn't know.
  // -- or --
  // Programmer forgot delete the var3.
}

I use a method to resolve memory leaks but I don't sure about it in complex situations.

My method is: Don't use any pointers (except one place), Just use references instead of pointers.

for example:

class my_class
{
  ...
};

my_class& func1()
{
  my_class* c = new my_class; // except one place.
  return *c;
}

void func2(my_class& p)
{
  ...
}

int main()
{
  my_class& var1 = func1();
  my_class  var2 = func1();

  my_class var3;
  func2(var3);

  // There is nothing to forget.
}

Does using references instead of pointers, resolve memory leaks?

Is it a good method for resolving memory leaks or there are better methods?


Edit:

Some answer of this question don't agree the below code don't have memory leak.

because it is a new question, I ask it seperately.

class my_class
{
  ...
};

my_class& func()
{
  my_class* c = new my_class;
  return *c;
}

int main()
{
  my_class& var1 = func();

  // I think there is no memory leak.
}

I ask it here: Does this code leak memory? (references, new, but no delete)

like image 622
Amir Saniyan Avatar asked Jul 21 '11 23:07

Amir Saniyan


People also ask

Is it better to use pointers or references?

References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.

How can you prevent memory leak in C which?

C. To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.

Are references more efficient than pointers?

It's much faster and memory-efficient to copy a pointer than to copy many of the things a pointer is likely to point to. A reference is stored in as many bytes as required to hold an address on the computer. This often makes reference much smaller than the things they refer to.

Are pointers safer than references?

References are Safer and Easier to Use References don't have to be dereferenced like pointers and can be used like normal variables (as an alias for some other variable). It is necessary to pass objects as a reference in a copy constructor.


3 Answers

You haven't resolved any memory leaks. If you new, then you must delete. All you did was dereference the pointer, it still needs to be deleted. You can resolve memory leaks by creating local objects and returning by value, or using smart pointers. 99 times out of 100, I prefer the return by value option.

Now, like many beginners, the idea of returning large objects by value probably scares your perf-centric mind. Read this to allay your fears.

like image 157
Benjamin Lindley Avatar answered Oct 31 '22 13:10

Benjamin Lindley


Your approach does not help at all:

Foo & magic()
{
  return * new Foo();  // dynamically allocated
}

int main()
{
  Foo x = magic();     // copied and lost
  Foo & y = magic();   // reference, still holding on
  delete &y;           // phew, and ewww
}

You are still just allocating the object dynamically and have to take care of it manually! In fact, my first usage makes a copy of the reference and then forgets the reference, creating an instant leak! And even if you did still somehow retain the reference, as in the second example, it becomes entirely unmanageable! (See Soap's comment.)

So please just forget about this entire idea quickly and look at resource managing containers instead!

For example:

#include <memory>

typedef std::shared_ptr<Foo> FooPtr;

FooPtr makeFoo()
{
  // return FooPtr(new Foo);       // Baby's first smart pointer
  return std::make_shared<Foo>();  // Adult approach to shared_ptr
}

int main()
{
  FooPtr pf = makeFoo();

  someFooModifier(*pf);
}
like image 25
Kerrek SB Avatar answered Oct 31 '22 15:10

Kerrek SB


Don't return raw pointers from functions; stick them in a smart pointer class such as unique_ptr or shared_ptr. Then you don't have to worry about deleting the allocated object.

Also, in your second example, who is deleting the object allocated by func1()? Just because you return a reference instead of pointer doesn't mean freeing of allocated memory will happen magically.

like image 27
Praetorian Avatar answered Oct 31 '22 15:10

Praetorian