John Smith
John Smith

Reputation: 3

c++ dynamic arrays in class

I am trying to create an example with dynamic arrays in class in order to user destructor and copy constructor.

The idea of the program is the following. I have class Labtest with two private fields. ntest an integer which holds the number of tests and values which is a dynamic array with ntest elements.

I added method add which extends dynamic array by one and add the argument to the last position.

I added a friend function max which returns the max element of the dynamic array (which takes a call by value object) in order to demonstrate the necessity of copy constructor.

I overloaded << in order to print the objects.

I added default constructor, destructor, copy constructor.

The code i wrote so far is

#include<iostream>
using namespace std;

class Labtest {
public:
    Labtest();
    Labtest(const Labtest& a);
    ~Labtest();
    friend int max(Labtest a);
    friend ostream& operator <<(ostream& out, const Labtest& lab);
    void add_value(int a);
private:
    int ntests;
    int *values;
};

int main()
{
    Labtest chem;
    chem.add_value(10); chem.add_value(20);
    cout << chem;
    cout << "the maximum is " << max(chem) << endl;
    cout << chem;
    return 0;
}

Labtest::Labtest() : ntests(0), values(NULL)
{
}

Labtest::Labtest(const Labtest & a) : ntests(a.ntests)
{
    values = new int(ntests);
    for (int i = 0; i < ntests; i++)
        values[i] = a.values[i];
}

Labtest::~Labtest()
{
    ntests = 0;
    delete[] values;
}

void Labtest::add_value(int a)
{
    int *newvalues = new int(ntests+1);
    for (int i = 0; i < ntests; i++)
        newvalues[i] = values[i];
    newvalues[ntests] = a;
    delete[] values;
    values = newvalues;
    ntests++;
}

int max(Labtest a)
{
    int m = a.values[0];
    for (int i = 1; i < a.ntests; i++)
        if (m < a.values[i])
            m = a.values[i];
    return m;
}

ostream& operator <<(ostream & out, const Labtest& lab)
{
    for (int i = 0; i < lab.ntests; i++)
        out << lab.values[i] << " ";
    out << endl;
    return out;
}

I am getting an out of heap bounds error in Visual Studio. Even if i comment the call to the max function.

Any help will be appreciated!

Upvotes: 0

Views: 2657

Answers (3)

Fran&#231;ois Andrieux
Fran&#231;ois Andrieux

Reputation: 29022

new int(10) makes an int variable with the value 10. new int[10] makes an array of 10 ints. From the context, it's clear that you intended to use the second form. The use of the first form would lead to undefined behavior when you called delete[] values; (since values would not be an array) or when you accessed any elements beyond the first.

Upvotes: 2

Red.Wave
Red.Wave

Reputation: 4249

Template class sdt::vector just provides the functionallity you need - without involving you in details of memory management. But in case want to practice memory management, I should remind you of 3 notes:

  1. Syntax of array new operator is new type_spec[n] not new type_spec(n). The syntax that you have used, just allocates a single object and initializes it with n. So disposing it with array delete operator leads to UB.

  2. It is safer to nullify the pointer after deleting.

  3. When defining any class that aquires system resources (heap memmory in this case), it is often nessary to define following functions:

    Copy contructor.

    Swap function.

    Move constructor, normally in terms of swap.

    Move/copy assinment operator or both, in terms of swap.

    Destructor in terms of swap.

Otherwise - sooner or later - violating 1 or more of the resource handling rules occurs due to forgetting implicit temporary object generation or other semantics. And abnormal behavior will take place.

Upvotes: 0

ScoJay
ScoJay

Reputation: 19

When you're allocating arrays you should do int *newvalues = new int[ntests+1]; using brackets instead []

Upvotes: 0

Related Questions