I came across a situation where I need to create a temporary object and pass it to a function. In C#, I would do something along the lines of Bar(new Foo(1, 2));
and be done with it, as C# does hold your hand a lot when it comes to memory management.
My question is, in C++, would doing the same, calling Bar(new Foo(1, 2));
create a memory leak, as new assigns memory but it is not freed (deleted) anywhere as no object is assigned to it.
Here's a sample code:
#include <iostream>
using namespace std;
struct Foo
{
int a;
int b;
Foo(int a, int b)
{
this->a = a;
this->b = b;
}
};
void Bar(Foo *obj)
{
cout << obj->a << ":" << obj->b << endl;
}
int main(int argc, char** argv)
{
Bar(new Foo(1, 2));
return 0;
}
Note: I could not find a way to detect/check for memory leaks on a Windows environment (something like Valgrind on Linux), so any suggestions regarding that would be welcome! (Windows 10, g++ compiler)