Will the below code cause memory leak in c++

前端 未结 7 1851
醉话见心
醉话见心 2020-12-05 08:13
class someclass {};

class base
{
    int a;
    int *pint;
    someclass objsomeclass;
    someclass* psomeclass;
public:
    base()
    {
        objsomeclass = so         


        
相关标签:
7条回答
  • 2020-12-05 08:44

    Everything you "new" needs to be deleted, or you'll cause a memory leak. So these two lines:

    psomeclass = new someclass();
    pint = new int(); 
    

    Will cause memory leaks, because you need to do:

    delete pint;
    delete psomeclass;
    

    In a finally block to avoid them being leaked.

    Also, this line:

    base temp = base();
    

    Is unnecessary. You just need to do:

    base temp;
    

    Adding the "= base()" is unnecessary.

    0 讨论(0)
提交回复
热议问题