Jack
Jack

Reputation: 258

Where do I have to free the memory after allocating?

I am trying to do some practice for memory allocation.

I have the below code which is working but have two questions.

Where do I have to use delete [ ] to free the memory after allocating?

Why is the output for this code at function when using show() function is CDcar?.

#include <cstdlib>
#include <new>
#include <iostream>
#include <cstring>
using namespace std;

class automobile {

    private:

        char (*function)[30];
        char *type;
        double speed;

    public:

        automobile ( );
        automobile (double , char *);
        void speed_up (double);
        void speed_down(double);
        const char * get_function ( ) const;
        void show ( );

};

automobile::automobile ( ) {

    speed = 0;
    function = new char [1][30];
    strcpy(function[1], "CD player with MP3");

    type = new char [4];
    strcpy(type, "car");

}

automobile::automobile(double spd, char * fn ) {

    int sz;

}

void automobile::show ( ) {

    cout << "This is a " << type << " and it has the following functions: " << function[1] << ", and its speed is " << speed << " km/h\n"; 

}

int main ( ) {

    automobile car;

    car.show ( );

    return 0;
}

this is the output:

This is a car and it has the following functions: CDcar, and its speed is 0 km/h

I thought the output shoud be this:

This is a car and it has the following functions: CD player with MP3, and its speed is 0 km/h

Please advise

Upvotes: 0

Views: 202

Answers (5)

RC.
RC.

Reputation: 28217

Your output is incorrect b/c of the following:

 speed = 0;
 function = new char [1][30];
 strcpy(function[1], "CD player with MP3");

This should be

 speed = 0;
 function = new char [1][30];
 strcpy(function[0], "CD player with MP3");

and when you output you should be cout'ing function[0] instead of function[1].

Having said this, you should almost always try to eliminate manual calls to new and delete. It helps with maintainability and it helps keep code exception safe. In this case you can get this for free by using vectors and strings provided by the standard C++ library. In a more general sense, you want to follow the RAII Idiom. This will help C++ and memory management from shaving a couple of years off your life during your studies/career.

Upvotes: 2

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234484

Where do I have to use delete [ ] to free the memory after allocating?

Ideally nowhere. new and delete are features of C++ that are not suitable for most code. They are error-prone and too low-level. They're only useful for basic building blocks.

The code shown could benefit from basic building blocks like std::string, std::vector.


The code shown also invokes undefined behaviour at least in one place:

function = new char [1][30];
strcpy(function[1], "CD player with MP3");

Arrays are 0-based, so function[1] is an out-of-bounds access.

Upvotes: 6

Sebastian
Sebastian

Reputation: 8164

  1. You should place the delete[] for function and type within the destructor ~automobile (you don't have one currently, so you'll have to create it).

  2. Regarding the output: Your character is array is not well defined. Consider using std::vector<string> for such things (much easier).

Upvotes: 2

Tony The Lion
Tony The Lion

Reputation: 63200

You should call delete[] in the destructor of your class.

//Called when your class is destroyed.
automobile::~automobile()
{
   delete[] function;
}

Upvotes: 4

aisbaa
aisbaa

Reputation: 10633

Inside ~automobile destructor.

Upvotes: 1

Related Questions