#include<iostream>
int* func_b() {
int *c = new int;
return c;
}
int main() {
int* b = func_b();
return 0;
}
I can see that memory leak would be there in above code because I am not freeing the memory allocated using "new".
Want to know what is the best way to avoid this leak? One solution is:
int* func_b() {
int *c = new int;
return c;
}
int main() {
int* b = func_b();
delete b;
return 0;
}
Is this optimal solution? I would be great if someone can suggest alternative way.