关于c++里new和delete[]的问题

xioacd99
  • 10

我花了两个多小时才调试它并检查代码错误。但是我仍然找不到它崩溃的原因。 如果删除第91行到第93行之间的代码,则源代码将能够在vs和vscode中运行,但在dev-c中失败。dev-c的反馈是程序接收到的信号sigsegv。 如果不删除,所有平台都无法正常工作? 有人可以告诉我原因和解决方法吗?

1 #include <iostream>
2 using namespace std;
3 template <class T>
4 class DynamicVector
5 {
6 private:
7    T *array;
8    unsigned mallocSize, numofItems;
9    int virtualZero;
10
11 public:
12    DynamicVector(int Vindex)
13    {
14        array = NULL;
15        numofItems = 0;
16        mallocSize = 0;
17       virtualZero = Vindex;
18    }
19    DynamicVector(const DynamicVector &another)
20    {
21        if (mallocSize < another.numofItems)
22        {
23            if (array)
24            {
25                delete[] array;
26                array = NULL;
27            }
28            array = new T[another.mallocSize];
29        }
30        virtualZero = another.virtualZero;
31        mallocSize = another.mallocSize;
32        numofItems = another.numofItems;
33        for (int i = 0; i < another.numofItems; i++)
34            *(array + i) = *(another.array + i);
35    }
36    ~DynamicVector()
37    {
38        if (array)
39        {
40            delete[] array;
41            array = NULL;
42        }
43    }
44    DynamicVector<T> &operator=(const DynamicVector<T> &another)
45    {
46        if (mallocSize < another.mallocSize)
47        {
48            delete[] array;
49            array = NULL;
50            array = new T[another.mallocSize];
51        }
52        virtualZero = another.virtualZero;
53        mallocSize = another.mallocSize;
54        numofItems = another.numofItems;
55        for (int i = 0; i < another.numofItems; i++)
56            *(array + i) = *(another.array + i);
57        return *this;
58    }
59    inline void push_back(const T &n)
60    {
61        if (numofItems < mallocSize)
62        {
63            *(array + numofItems) = n;
64        }
65        else if (numofItems == mallocSize)
66        {
67            T *num = new T[numofItems+1];
68            for (int i = 0; i < numofItems; i++)
69                *(num + i) = *(array + i);
70            if (array)
71            {
72                delete[] array;
73                array = NULL;
74            }
75            array = new T[2 * mallocSize + 1];
76            mallocSize = 2 * mallocSize + 1;
77            for (int i = 0; i < numofItems; i++)
78                *(array + i) =*(num+i);
79            *(array + numofItems) = n;
80            delete[] num;
81            num = NULL;
82        }
83        numofItems++;
84    }
85    void push_back(const DynamicVector<T> &another)
86    {
87        T *num = new T[numofItems+1];
88        for (int i = 0; i < numofItems; i++)
89            *(num + i) = *(array + i);
90        if (array) {
91            delete[] array;
92            array = NULL;
93        }
94        array = new T[mallocSize + another.mallocSize];
95        mallocSize = mallocSize + another.mallocSize;
96        for (int i = 0; i < numofItems; i++)
97            *(array + i) = *(num + i);
98        delete[] num;
99        num = NULL;
100        for (int i = numofItems, j = 0; i < numofItems + another.numofItems; i++,j++)
101            *(array + i) = *(another.array+j);
102        numofItems = numofItems + another.numofItems;
103    }
104    T &operator[](int Vindex)
105    {
106     int _entry = Vindex - virtualZero;
107        if (_entry < 0 || _entry >= numofItems)
108      {
109            cout << endl
110                 << "Out Of Range";
111            exit(1);
112        }
113        return array[_entry];
114    }
115
116    bool operator==(const DynamicVector<T> &dv) const
117    {
118        if (virtualZero == dv.virtualZero)
119        {
120            for (int i = 0; i < numofItems; i++)
121                if (*(array + i) != *(dv.array + i))
122                    return false;
123            return true;
124        }
125        else
126            return false;
127    }
128    unsigned length() const
129    {
130        return numofItems;
131    }
132    unsigned capacity() const
133    {
134        return mallocSize;
135    }
136    int firstIndex() const
137    {
138        return virtualZero;
139    }
140 };
int main()
{

    DynamicVector<int> ra(-2);
    int i, n;
    cin >> n;
    ra.push_back(-3);
    ra.push_back(-2);
    ra.push_back(-1);
    for (i = 0; i < n; i++)
    {
        ra.push_back(i);
    }
    cout << "\n malloSize is " << ra.capacity();
    cout << "\n numofItems is " << ra.length();
    cout << "\n StartIndex is " << ra.firstIndex() << endl;
    for (i = -2; i < n + 1; i++)
    {
        cout << ra[i] << " ";
    }
    cout << endl;
    DynamicVector<int> raCopy(ra);
    cout << "\n malloSize is " << raCopy.capacity();
    cout << "\n numofItems is " << raCopy.length();
    cout << "\n StartIndex is " << raCopy.firstIndex() << endl;
    cout << endl;
    for (i = -2; i < n + 1; i++)
    {
        cout << ++ra[i] << " ";
    }
    cout << endl;
    for (i = -2; i < n + 1; i++)
    {
        cout << raCopy[i] << " ";
    }

    raCopy = ra;
    if (ra == raCopy)
        cout << "\n ra == raCopy";
    else
        cout << "\n ra != raCopy";

    ra[-2] = 100;

    if (ra == raCopy)
        cout << "\n ra == raCopy";
    else
        cout << "\n ra != raCopy";

    raCopy.push_back(ra);
    cout << endl;
    int firstI = raCopy.firstIndex();
    for (i = 0; i < raCopy.length(); i++)
    {
        cout << raCopy[i + firstI] << " ";
    }
    system("pause");
    return 0;
}
回复
阅读 827
1 个回答
✓ 已被采纳

1.指针问题很多都在delete的时候才暴露出来,但是往往导致错误的原因在之前的代码,这也是内存错误难以解决的原因之一,例如如果对DynamicVector构造之后立即push_bacK会发生什么?你可以自己看一下。
2.建议你做好测试工作,尤其你现在的水平还不足以比较轻松的理清楚这个类的逻辑。(如果目前没有接触过单元测试的话,就手工的每实现一个方法就写测试代码测试你的方法有没有漏洞),尽量不要一下添加很多东西然后才测试,这样出了问题不知道在哪找。
3.DynamicVector的数据成员的耦合度是非常高的,每改变一个成员的值就要考虑其他成员是否还与其保持逻辑上的一致,你的复制构造函数里怎么会只更新了mallocSize而不改变array呢,这养mallocSize还能够代表array的数组长度么?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏