Is the allocated memory new automatically freed?

I'm 99% sure that the answer to this dazzling no . Please acknowledge my suggestion that the following code will cause a memory leak.

Data &getData()
{
    Data *i = new Data();
    return *i;
}

void exampleFunc()
{
    Data d1 = getData();
    Data d2;

    /* d1 is not deallocated because it is on the heap, and d2 is
     * because it is on the stack. */
}

Note that this is a simplified example, so you obviously would not use this code. Therefore, do not point out this.

Update 1:

To add to this, what if I assign a pointer to a link? In this case, I assume that the data is not copied ...

Data &getData()
{
    Data *i = new Data();
    return *i;
}

void exampleFunc()
{
    // Does copying occur here?
    Data &d1 = getData();

    // Does this deallocate the memory assigned to the pointer?
    delete &d;
}

Update 2:

I think in order to answer my own question (in Update 1), the following code proves that assigning a link to a link does not cause a copy ...

#include <iostream>
#include <string>

using namespace std;

class Data
{
public:
    string mName;

    Data(const string &name) : mName(name)
    { cout << mName << " default ctor" << endl; }

    Data(const Data& other)
    {
        mName = other.mName + " (copy)";
        cout << mName << " copy ctor" << endl;
    }

    ~Data()
    { cout << mName << " dtor" << endl; }

    static Data &getData(const string &name)
    {
        Data *d = new Data(name);
        return *d;
    }
};

int main()
{
    cout << "d1..." << endl;
    Data d1 = Data::getData("d1");

    cout << "d2..." << endl;
    Data d2("d2");

    cout << "d3..." << endl;
    Data &d3 = Data::getData("d3");

    cout << "return..."  << endl;
    return 0;
}

Gets the following result ...

d1...
d1 default ctor
d1 (copy) copy ctor
d2...
d2 default ctor
d3...
d3 default ctor
return...
d2 dtor
d1 (copy) dtor

Eric Melski ( 2 - exmaple).

+5
7

d1 d2 , . , , - Data, getData(). , Data . :

class Data {
public:
    Data() { cout << "Data default ctor" << endl; }
    Data(const Data& other) { cout << "Data copy ctor" << endl; }
    ~Data() { cout << "Data dtor" << endl; }

    static Data& getData()
    {
        Data *i = new Data();
        return *i;
    }
};

, Data. , Data d1 = getData();, , , . , :

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    Data d1 = Data::getData();
    Data d2;

    return 0;
}

:

Data default ctor
Data copy ctor
Data default ctor
Data dtor
Data dtor

, :

  • , Data getData().
  • d1 Data, .
  • d2.
  • d2, main().
  • d1, main().

, , - , Data.

, ,

+24

d1 - , , getData(). d1 , , getData(), .

+8

d1 d2 - , , , getData() , . d1 , d1 exampleFunc, , getData(), .

, getData() , , -, , .

, :

Data& d = getData();
delete &d;
+3

, , - . , , , ++ .

+3

" " Java/#.

++ , :

Data getData()
{
    Data i;
    return i;
}

void exampleFunc()
{
    Data d1 = getData();
    /* d1 is constructed here and destroyed */
    Data d2;

}

, , , , auto_ptr, :

auto_ptr<Data> getData()
{
    auto_ptr<Data> i(new Data());
    return i;
}

void exampleFunc()
{
    auto_ptr<Data> d1 = getData();
    /* now d1 is destroyed when goes out of scope */
    Data d2;
}
+1

. , delete delete [].

0

, - .

:

int main()
{
Data* d = new Data();
return 0;   //end of execution
}

, d, .

, , , , (), ( ) .

0

All Articles