简体   繁体   English

listnode循环,分段错误

[英]listnode loop, segmentation fault

Haven't managed to find a simplified version of this problem. 尚未找到此问题的简化版本。

I'm new to listnodes so I probably just don't understand something simplea bout them. 我是listnode的新手,所以我可能只是不了解简单的东西。 Everything seems to work until I pass my for loop. 在我通过for循环之前,一切似乎都可以正常工作。

while(temp2 != NULL){

    for(int i = temp2->data; i > 0; i--){
    //////////////
    temp1 = head;
    temp3 = Product.head;

        while(temp1 != NULL || temp3 != NULL){
            cout << "\ntemp1->data: " << temp1->data;
            cout << "\ntemp3->data: " << temp3->data;


            temp1 = temp1->next;
            temp3 = temp3->next;
        }
    /////////////
    cout << "\n\ndecrement: " << i;
    }

temp2 = temp2->next;    
}

Everything runs except between the comments. 除了注释之间的所有内容,其他所有内容都运行。

My thought was that I could traverse to the end of the listnode (temp1), then once I reached the end I point back to the head and traverse through it as many times as necessary. 我的想法是,我可以遍历listnode(temp1)的末尾,然后一旦到达末尾,我便指向头并遍历该遍遍。 I'm getting a segmentation fault there though. 不过我遇到了细分错误。

I cant traverse through temp2 just fine, what am I doing wrong though when it comes to traversing through temp1 multiple times? 我无法遍历temp2,但是多次遍历temp1时,我在做什么错呢? Can I only refer to the head of the list once? 我可以只列出一次清单的头吗?

edit: I figured out that I just left a conditional in. Now Its my 3rd list that is giving me the segmentation fault. 编辑:我发现我只是留了一个条件。现在它是我的第3个列表,这给了我分割错误。 it will run for a single loop but once out of the while loop it breaks. 它会运行一个循环,但一旦退出while循环,它就会中断。

Think about it like this. 这样想吧。 In a specific iteration of your while loop, temp1 points to an address and temp1->next points to NULL . 在while循环的特定迭代中, temp1指向一个地址, temp1->next指向NULL temp3 points to an address and temp3->next also points to an address. temp3指向一个地址,而temp3->next也指向一个地址。

temp1 == address

temp1->next == NULL

temp3 == address

temp3->next == NULL

You iterate through the current address, printing the data from temp1 and temp3 . 您遍历当前地址,打印temp1temp3的数据。 Then you set them equal to their next pointers. 然后,将它们设置为等于其下一个指针。 Now temp1 points to NULL and temp3 points to an address. 现在, temp1指向NULLtemp3指向一个地址。

Your condition of the while loop checks to see if either of the pointers do not point to NULL . while循环的条件检查是否有两个指针没有指向NULL Because temp3 does not point to NULL , it will try to print out the data for temp1 and temp3 . 由于temp3不指向NULL ,它将尝试打印出temp1temp3的数据。 But this will cause a problem because temp1 points to NULL and you are trying to dereference a NULL pointer, which will cause a segmentation fault. 但这会引起问题,因为temp1指向NULL并且您正试图取消引用NULL指针,这将导致分段错误。

To solve this issue, you should use an AND (&&) condition for your while loop so that it only prints when temp1 AND temp3 don't point to NULL . 要解决此问题,您应该为while循环使用AND(&&)条件,以便仅在temp1temp3不指向NULL temp3

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

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