简体   繁体   English

当我制作 class 模板时,赋值运算符崩溃代码

[英]Assignment operator crashes code while i make template of class

I'm making a template for vector class in which I'm making vector from an array.我正在为向量 class 制作模板,其中我正在从数组制作向量。 But within the class the assignment operator is crashing the code as I can see the return value is some ( Process returned -1073741819 (0xC0000005)).但是在 class 中,赋值运算符正在使代码崩溃,因为我可以看到返回值是一些(进程返回 -1073741819 (0xC0000005))。

int i=0;
const int size = 4;
template<class T>
class vector{
public:
    vector()
    {
        x = new T[size];
       for(i=0;i<size;i++)
            x[i] =0 ;
    }
    vector( T *a)   // This part is creating issue if i comment it out code 
// successfully return with value Zero. But with this it doesn't work at all.
    {
      //  cout<<"called";
        for(i=0;i<size;i++)
        {
            x[i] = a[i];
        }
    }
    T  operator *(vector &y)
    {
        T sum = 0;
        for(i=0;i<size;i++)
        {
            sum += this->x[i] + y.x[i];
        }
        return sum;
    }

private:
    T * x; // Type of vector;

};
int main()
{
    int arr1[4] = {2,3,4,5};
    int arr2[4] = {6,7,8,9};
    vector<int> v1;
    vector<int>v2;
    v1 = arr1;  // This call is the culprit. it doesn't work t all.
    v2 = arr2;

    return 0;
}```

You have several issues that lead to the crash and other problems.您有几个问题会导致崩溃和其他问题。

The first is that the statement首先是声明

v1 = arr1;

is equivalent to相当于

v1 = vector<int>(&arr1[0]);

That means a temporary vector<int> object will be created, which invokes the vector(T *a) constructor.这意味着将创建一个临时vector<int> object,它调用vector(T *a)构造函数。

And here the problems start: Because it's a constructor, the object being constructed is uninitialized , as it's the purpose of the constructor to initialize it.问题从这里开始:因为它是一个构造函数,正在构造的 object 是uninitialized ,因为构造函数的目的是初始化它。 That means the member variable x will also be uninitialized and will not be pointing to a valid location.这意味着成员变量x将未初始化并且不会指向有效位置。

You need to allocate memory and make x point to that memory, otherwise you dereference the uninitialized pointer which leads to undefined behavior and very likely your crash.您需要分配 memory 并使x指向该 memory,否则您取消引用未初始化的指针,这会导致未定义的行为并且很可能会导致崩溃。


As for the other problems, first of all you miss a destructor, which means that memory allocations don't have anywhere to be free'd.至于其他问题,首先你错过了一个析构函数,这意味着 memory 分配没有任何地方可以释放。 Which in this case actually is good, because since you don't have an actual assignment operator (and rely on the compiler generated assignment operator) the copying in v1 = arr1 will be a shallow copy, that copy only the pointer itself and not the memory it points to.在这种情况下实际上是好的,因为由于您没有实际的赋值运算符(并且依赖于编译器生成的赋值运算符),因此v1 = arr1中的复制将是一个浅拷贝,它只复制指针本身而不是memory 它指向。

The shallow copy problem means that for a while you will have two objects whose x member will point to the same memory.浅拷贝问题意味着有一段时间你会有两个对象,它们的x成员将指向同一个 memory。 And if one object deletes that memory (like when the temporary object mentioned above is destructed) then the second pointer in v1 will no longer be valid.如果一个 object 删除了那个 memory(比如上面提到的临时 object 被破坏时),那么v1中的第二个指针将不再有效。

This is the reason behind the rules of three and five .这就是三五规则背后的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM