Domanda

I'm very new to C++ and I wish to make clear some points regarding memory management using the operator "new ..." and the operator "delete ...".

I will post some code of mine, and I ask if you please would correct my comments if they are wrong.

I'm also dealing with virtual functions and interface, which is clear by reading the code, and I also ask you if i'm approaching them the right way.

Then I have a more direct question, when should I use "new[] ..." or "delete[] ...", and how should I use them correctly?

PS: the output of code below is:

car built
motorcycle built
car has 4 wheels
motorcycle has 2 wheels
car destroyed
motorcycle destroyed

That's the main.cpp source:

#include <iostream>

using namespace std;

class vehicle
{
    public:
        virtual
        ~vehicle()
        {
        }

        virtual void
        wheelNum() = 0;
};

class car : public vehicle
{
    public:
        car()
        {
            cout << "car built" << endl;
        }

        ~car()
        {
            cout << "car destroyed" << endl;
        }

        void
        wheelNum()
        {
            cout << "car has 4 wheels" << endl;
        }

};

class motorcycle : public vehicle
{
    public:
        motorcycle()
        {
            cout << "motorcycle built" << endl;
        }

        ~motorcycle()
        {
            cout << "motorcycle destroyed" << endl;
        }

        void
        wheelNum()
        {
            cout << "motorcycle has 2 wheels" << endl;
        }

};

int
main()
{
    // motorVehicle[2] is allocated in the STACK and has room for 2 pointers to vehicle class object

    // when I call "new ...", I allocate room for an object of vehicle class in the HEAP and I obtain its pointer, which is stored in the STACK

    vehicle* motorVehicle[2] = { new (car), new (motorcycle) };

    for (int i = 0; i < 2; i++)
    {
        // for every pointer to a vehicle in the array, I access the method wheelNum() of the pointed object

        motorVehicle[i] -> wheelNum();
    }

    for (int i = 0; i < 2; i++)
    {
        // given that I allocated vehicles in the HEAP, I have to eliminate them before terminating the program

        // nevertheless pointers "motorVehicle[i]" are allocated in the STACK and therefore I don't need to delete them

        delete (motorVehicle[i]);
    }

    return 0;
}

Thanks you all.

È stato utile?

Soluzione

Memory allocated with new is on the HEAP, everything else in on the stack. So in your code, you have

vehicle* motorVehicle[2] = { new (car), new (motorcycle) };

On the stack there is an array of two pointers vehicle*[2], and on the heap are two objects, a car and a motocycle.

Then you have two loops

for (int i = 0; i < 2; i++)

each of which create an integer on the stack for the duration of the loop.

Altri suggerimenti

Concerning your code: the array of pointers is a local variable, which would be allocated on the stack. What the pointers them selves point to is, in the case of your example, allocated dynamically (on the heap).

Concerning the "more direct question": I've yet to find any case where new[] should be used. It's present for reasons of completeness, but it doesn't really have any reasonable use.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top