c++ - 删除数组会导致运行时错误。 C++

标签 c++ arrays resize

我有一个有序数组列表。在我的调整大小函数中,我创建了一个新数组并为其分配旧数组的值,然后我使用 delete[] arrayname; 删除了旧数组。 每当调整大小功能发挥作用时,这都会在运行时导致错误。 dbgheap.c 被调用。有人以前见过这个吗?

这是我的代码:

//--------------------------------------------------------------------------------------------
//  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 和 g_size 由用户在程序开始时给定其值。

最佳答案

这里可能还有其他问题,但我能看到的最明显的是:

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

如果 pos 恰好为零,你最终会这样做:

    m_array[0] = m_array[-1];

这个问题会立即显示(当 num_elements 为零时 - 您还没有显示构造函数,但我希望您已初始化所有内容)。

把循环中的>=改成>或许可以解决你所有的烦恼。

从概念上讲,你应该同意这个逻辑。当您正要用新的替换它时,将 before m_array[pos] 项目向前移动到 m_array[pos] 是没有意义的项目。


[编辑] 现在您已经发布了您的构造函数:

m_array = new Datatype[p_size,grow_size];   

由于逗号运算符的工作方式,这将使用大小 grow_size 而不是 p_size 来初始化您的数组。读这个:How does the Comma Operator work

请改为这样做:

m_array = new Datatype[p_size];

关于c++ - 删除数组会导致运行时错误。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15371654/

相关文章:

android - 调整 android 标签的大小

c++ - 可变变量内存位置

c++ - 在 C++ 中使用 memcpy 将结构写入 char 数组

c++ - 跨模板特化共享的静态成员

arrays - 如何在postgresql中找到任意大小数组的所有组合(子集)

arrays - 通过递归查找数组中的最大值

java - 关于java和数组在方法中搜索并返回数据

database - 如何在 Scala 中调整数组的大小

c++ - Qt 防止控件在调整窗口大小时移动

javascript - 如何将图像缩放并居中到正方形尺寸?