错误:删除struct以释放内存会产生分段错误(c ++)

时间:2017-02-01 02:16:22

标签: c++

这是数组倍增器的代码。 list1和list2是结构。在本程序的其余部分中,list1必须加倍7次。在第一次运行中,length = 100,list2 length = 200;我刚刚学会了指针,所以我可能只是在最后有一个简单的错误。删除list1后立即发生分段错误;

我也不知道它是否相关,但结构和构造函数存储为全局变量。

if(length - total == 1){

        length=length*2;
        wordList *list2 = new wordList [length];

        for(int i=0;i<length/2;i++){ //allocate old array to newly created list2 which was formerly pointing towards the 4 array
                list2[i]=list1[i];
            }
        delete list1;
        //segmentation fault here
        list1=list2; //I am trying to get pointer list1 to point to what pointer is list2 assigned to as well

        }

0 个答案:

没有答案