class example
{
public:
int div(int a, int b)
{
cout << a/b;
}
};
example * init()
{
example *a;
a = new example;
return a;
}
int main
{
example *k;
k = init();
k->div(5,7);
delete k;
}
OK! I was unable to understand how it wasn't a memory leak.After running it step by step through a debugger, I understood what is happening...
Pointers are confusing!!!
Well, there could be a memory leak if there was an exception between the new and the delete that would exit from main, but the OS would have freed it when the process is closed.
Generally, the memory leaks which are important are those in loops, that constently increase memory usage while the program is running. The leaks of one variable once in the program lifetime rarely have much effect (unless is one big allocation of memory of course)