【问题标题】:Segmentation fault class List分段故障类列表
【发布时间】:2020-07-21 08:25:41
【问题描述】:

我定义了以下类列表。 我正在尝试通过覆盖我在类中声明的 [] 运算符来显示所有元素

template <typename Data>
class List{
protected:    
   ulong size = 0;

    struct Node{
        Data value;
        struct Node *next;

        //...
    }

    struct Node *top = nullptr;

public :
    //...
    Data& operator [](ulong) const;
    //...
};

//Specific Constructor
template <typename Data>
List<Data>::List(ulong newSize, Data value){
    size = newSize;
    struct Node* tmp;
    struct Node* tmp2 = nullptr;

    for(ulong i = 0; i < newSize; i++){
        tmp2 = new struct Node(value);
        if(top == nullptr){
            top = tmp2;
        }else{
            tmp = top;
            while(tmp->next != nullptr){
                tmp = tmp->next;
            }
            tmp->next = tmp2;
        }
    }
}

 //Copy Constructor
template <typename Data>
List<Data>::List(const List<Data>& ref){
    size = ref.size;
    struct Node* tmp = ref.top;
    struct Node* tmp2 = nullptr;
    struct Node* tmp3;

    while(tmp != nullptr){
        tmp2 = new struct Node();
        tmp2->value = tmp->value;
        if(top == nullptr){
            top = tmp2;
        }else{
            tmp3 = top;
            while(tmp3->next != nullptr){
                tmp3 = tmp3->next;
            }
            tmp3->next = tmp2;
        }
        tmp = tmp->next;
    } 
}

// Move Constructor
template <typename Data>
List<Data>::List(List<Data>&& ref){
    std::swap(size, ref.size);
    std::swap(top, ref.top);
}

template <typename Data>
Data& List<Data>::operator [](ulong i) const{
    if(size == 0)
        throw std::length_error("Lista vuota!");
    else if(i >= size)
        throw std::out_of_range("Lista troppo corta!");

    struct Node* tmp = top;
    for(ulong index = 0; index <= i-1; index++)
        tmp = tmp->next;

    return tmp->value;
}


int main(){
    List<int> l1(10,5); //List of 10 elements all equals to 5

    for(ulong i = 0; i < l1.getSize(); i++)
        std::cout << l1[i] << std::endl;

     return 0;
}

(我把最重要的东西放在这个问题上) 我在分配期间没有收到任何错误(例如:l1[3] = 20;),但在这种情况下,我得到了一个 sigFault,我不明白为什么。 感谢您的帮助。

【问题讨论】:

  • 你能展示你的构造函数的实现吗?
  • 另外,index &lt;= i-1 也会变得很糟糕,因为 indexi 分别是 ulongsee here
  • main()中,你写的是for(ulong i &lt; 0; i &lt; l1.getSize(); i++),你的意思是for(ulong i = 0; i &lt; l1.getSize(); i++)吗?
  • 你在使用调试器吗?到目前为止,cmets 中建议的所有提示都可以通过使用调试器轻松发现。
  • operator[]() 假设可以从top 遍历(即tmp = top 然后执行tmp = tmp-&gt;next 总共i 次,其中i0size)。如果沿途的任何tmp-&gt;next 为空或未初始化,则行为未定义。所有构造函数都需要确保在创建对象时为真,而所有其他更改对象的函数(包括operator=())需要确保它保持为真。如果您使用调试器单步执行,我敢打赌您会发现复制构造函数没有正确设置的情况。

标签: c++ list pointers segmentation-fault


【解决方案1】:

operator[] 中的 for 循环使用了不适当的条件。对于 index &lt;= i-1 的无符号比较,以及 i 的值为 0,条件为 index &lt;= unsigned(-1),这将始终为真。

条件可以改写为index &lt; i:

for (ulong index = 0; index < i; index++)
    tmp = tmp->next;

或者你的循环可以重写为一个while循环:

while (i-- > 0)
    tmp = tmp->next;

【讨论】:

    猜你喜欢
    • 2014-01-29
    • 2018-01-19
    • 1970-01-01
    相关资源
    最近更新 更多