问答

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

作者:admin 2021-08-21 我要评论

我花了两个多小时才调试它并检查代码错误。但是我仍然找不到它崩溃的原因。 如果删除第91行到第93行之间的代码,则源代码将能够在vs和vscode中运行,但在dev-c中...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

我花了两个多小时才调试它并检查代码错误。但是我仍然找不到它崩溃的原因。 如果删除第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;
}
###

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

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 使用箭头函数return数据不能实现

    使用箭头函数return数据不能实现

  • 个人小程序号能接入小程序3D地图吗?

    个人小程序号能接入小程序3D地图吗?

  • vue 移动端项目中,使用了better-scrol

    vue 移动端项目中,使用了better-scrol

  • css如何做出区域平均划分为四列

    css如何做出区域平均划分为四列

腾讯云代理商
海外云服务器