I have a code such as this:
typedef std::shared_ptr<int> BUFFER_TYPE
class MyClass
{
BUFFER_TYPE buffer;
public:
MyClass(int n)
{
buffer=std::make_shared<int> (n);
}
MyClass()
{
buffer=null;
}
BUFFER_TYPE GetBuffer()
{
return buffer;
}
}
MyClass GetMyClass()
{
MyClass x(200);
return x;
}
void SetMyClass(MyClass cl)
{
MyClass y=cl;
BUFFER_TYPE buffer=y.GetBuffer();
buffer[20]=100;
}
void main()
{
MyClass c(100);
MyClass d=c;
MyClass e=GetMyClass();
SetMyClass(e);
BUFFER_TYPE buffer=e.GetBuffer();
printf("Buffer is %d\n",buffer[20]);
}
Is there any memory leak with this code?
Is it working properly?
Am I using shared_ptr correctly?
Please note that buffer should be a pointer to an array of say 100 int and not one integer.