You are on page 1of 1

What are ways to avoid memory leaks?

Answer
A memory leak is the effect of running out of memory.

A memory leak is what happens when you forget to free a block of memory allocated
with the new operator or when you make it impossible to so.

The measures you can take are :


Delete before reallocating a memory

Be sure you have a pointer to each dynamic variable so that you do not lose a location
that you need to free.

Avoid these combinations : malloc() - delete and new - free()

Avoid these combinations : new - delete [] and new [] – delete

What is the difference between dynamic and static casting?


Answer
static_cast are used in two cases:

1) for implicit casts that the compiler would make automatically anyway (bool to int)
2) as a mandatory forced cast (float to int).

dynamic_cast is unique to C++. It is used at runtime when info is required to make the
proper cast. For eg, when you downcast a base class pointer to a derived class.

You might also like