Becca
Becca

Reputation: 117

Deleting array causes run-time error. C++

I have an ordered array list. And in my resize function I create a new array and assign it the values of the old array and then I delete the old array using delete[] arrayname;. This causes an error at run-time whenever the resize function comes into play. dbgheap.c is called. Has anyone ever seen this before?

Here is my code:

//--------------------------------------------------------------------------------------------
//  Name:           OrderedArray.h.
//  Description:    Header file for the use in OrderedArray.cpp.
//--------------------------------------------------------------------------------------------
#ifndef ORDEREDARRAY_H
#define ORDEREDARRAY_H
#include <iostream>

using namespace std;
//--------------------------------------------------------------------------------------------
//  Name:           template <class Datatype>   
//  Description:        
//--------------------------------------------------------------------------------------------
template <class Datatype>
//--------------------------------------------------------------------------------------------
//  Class: OrderedArray.            
//--------------------------------------------------------------------------------------------
class OrderedArray
{
//--------------------------------------------------------------------------------------------
//  Member Variables.           
//--------------------------------------------------------------------------------------------
private:
Datatype* m_array;
int size;
int g_size;
int num_elements;   //Counter for the number of elements in the Array.

void Resize(int p_size)//resizes the array to the size of p_size
    {
        cout << "Did i get this far ";
        if(p_size < 0)//checks if new size is less than 0
        {
            cout << "ERROR! Size of an array can not be less than 0!" << endl;
        }
        else//else its ok to continue
        {
            Datatype* newArray = new Datatype[p_size];//creates a pointer newArray that points at a new array
            if(newArray == 0)
            {
                return;
            }
            cout << "Did i get this far ";
            int min;

            if(p_size < size)//checks the if the new array is smaller than the old one
                min = p_size;
            else//else its going to be bigger
                min = size;
            cout << "Did i get this far ";
            int index;
            int temp = num_elements;//puts num_elements into a temporary variable called temp
            num_elements = 0;//num_elements is set to 0
            for(index = 0; index < min; index++)
            {
                newArray[index] = m_array[index];//places everything from the old array into the new array that will fit.
                if(num_elements < temp)//if the num_elements is less than temp(the original num_elements)
                {
                    num_elements++;//increment num_elements. This will keep incrementing to create the new num_elements based the number of elements cut off in the resize
                }
            }
            size = p_size;//sets the old size to be equal to the new size
            cout << "Did i get this far ";
            if(m_array != 0)
            {
            cout << "\nI am just about to delete ";
            delete[] m_array;//deletes the old array
            }
            m_array = newArray;//makes m_array point at the new array
            newArray = 0;//makes newArray a null pointer
        }
    }
//---------------------------------------------------------------------------------------
// Name:             Push
// Description:      
//---------------------------------------------------------------------------------------
void push(Datatype p_item)
{
    if(num_elements == size)//checks if the array is full and needs to be resized
    {
        Resize(size + g_size);//calls the resize function
    }

    int pos = num_elements;
    for(int x=0;x<num_elements;x++)
    {
        if(p_item < m_array[x])
        {
        pos=x;
        }
    }

    //loops through the array from high to low moving all values to the right
    //to make space for the passed in value until it gets to the right place
    for(int index = num_elements; index >= pos; index--)
    {
        m_array[index] = m_array[index-1];//moves the values to the right
    }
        m_array[pos] = p_item;//the passed in value is positioned into its ordered position
        num_elements++;

    cout<< "Num Elements " << num_elements;
    cout<< "Size " <<size;
}

    //--------------------------------------------------------------------------------------------
    //  Name:           Constructor.
    //  Description:    Constructs the Array.
    //--------------------------------------------------------------------------------------------
    OrderedArray(int p_size, int grow_size)
    {
        //Sets the Array size.
        m_array = new Datatype[p_size,grow_size];   
        size = p_size;
        g_size = grow_size; 
        //How many elements are in the Array.
    num_elements = 0;               
}

//size and g_size are given its value by the user at the start of the program.

Upvotes: 0

Views: 2116

Answers (2)

Pendo826
Pendo826

Reputation: 1002

Your code m_array = new Datatype[p_size,grow_size]; in the constructor should only take one parameter which is the size of the array.

Change to this: m_array = new Datatype[p_size];

Upvotes: 1

paddy
paddy

Reputation: 63481

There may be some other issue here, but the most obvious thing I can see is this:

for(int index = num_elements; index >= pos; index--)
{
    m_array[index] = m_array[index-1];
}

If pos happens to be zero, you will eventually do this:

    m_array[0] = m_array[-1];

This problem will show immediately (when num_elements is zero - you haven't shown your constructor, but I do hope that you initialised everything).

Changing >= to > in the loop may solve all your troubles.

Conceptually, you ought to agree with this logic. There is no point moving the item before m_array[pos] forward to m_array[pos] when you are just about to replace it with the new item.


[edit] Now that you have posted your constructor:

m_array = new Datatype[p_size,grow_size];   

This will initialise your array with the size grow_size instead of p_size, because of how the comma operator works. Read this: How does the Comma Operator work

Please do this instead:

m_array = new Datatype[p_size];

Upvotes: 4

Related Questions