-3
    #include<iostream>

using namespace std;

class darray
{

private:
    int n; // size of the array
    int *a; // pointer to the 1st element

public:
    darray(int size)
    {
        n = size;
        a = new int[n];
    }

    ~darray(){ delete[] a; }

    void get_input();
    int get_element(int index);
    void set_element(int index, int value);
    int count(){ return n; }
    void print();
};


void darray::get_input()
{
    for (int i = 0; i < n; i++)
    {
        cin >> *(a + i);
    }
}

int darray::get_element(int index)
{
    if (index == -1)
        index = n - 1;
    return a[index];
}

void darray::set_element(int index,int value)
{
    a[index] = value;
}

void darray::print()
{
    for (int i = 0; i < n; i++)
    {
        cout << a[i];
        if (i < (n - 1))
            cout << " ";
    }
    cout << endl;
}

// perform insertion sort on the array a
void insertion_sort(darray d)
{
    int v = d.get_element(-1); // v is the right-most element
    int e = d.count() - 1; // pos of the empty cell
    // shift values greater than v to the empty cell
    for (int i = (d.count() - 2); i >= 0; i--)
    {
        if (d.get_element(i) > v)
        {
            d.set_element(e,d.get_element(i));
            d.print();
            e = i;
        }
        else
        {
            d.set_element(e, v);
            d.print();
            break;
        }

    }
}

int main()
{
    int s;
    cin >> s;
    darray d(s);
    d.get_input();
    insertion_sort(d);
    system("pause");
    return 0;
}

I use the darray class to make a array of size n at runtime. This class gives basic functions to handle this array. This programs says debugging assertion failed at the end. It gives this error after ruining the program.Other than that the program works fine. What is the reason for this error ?

The Mean Square
  • 234
  • 1
  • 9
dilin993
  • 83
  • 4
  • 9
    Yawn. Rule of Three. Yawn. Pass by reference. – Kerrek SB Oct 14 '14 at 12:55
  • 1
    You forgot the [Rule of Three](http://stackoverflow.com/questions/4172722) (so your class can't be safely copied); and `insertion_sort` takes its argument by value instead of by reference (so it won't do anything useful even if the copy were safe). Use `std::vector` instead of juggling a pointer, or write a copy constructor and copy-assignment operator if you want to make life difficult for yourself. – Mike Seymour Oct 14 '14 at 12:56
  • And, if you continue on with this array, beware of that for (int i = 0; i < n; i++) { cin >> *(a + i); } If you are trying to read one character at a time into that array, this for loop might end up in a buffer overflow. – KBi Oct 14 '14 at 12:57
  • I just want to input n in number of integers using for (int i = 0; i < n; i++) { cin >> *(a + i); }. Would this give a buffer overflow ? What's a better way input some integers separated by spaces ? – dilin993 Oct 15 '14 at 16:36

1 Answers1

0

You need to declare and define a copy constructor:

darray::darray(const darray& src)
{
    n = src.n;
    a = new int[n];
    for (int i = 0; i < n; i++)
    {
        *(a + i) = *(src.a + i);
    }
}
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69