C ++结构指针Segfault [英] C++ Struct Pointer Segfault

查看:149
本文介绍了C ++结构指针Segfault的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,感谢所有回覆这则讯息的人。

First, thanks in advance for all of you who respond to this post.

其次,我浏览了所有其他讯息,

Second, I've looked through all other posts and couldn't find anything that helped me (my apologies, I'm new to C++).

这是我的代码:

Node* Insert(Node *head,int data) //for linked lists
{
  Node* current = head;
  while(current -> next != NULL){
      current = current -> next;
  }
  cout << head -> data;
  Node *last = new Node();
  last -> data = data;
  last -> next = NULL;
  current -> next = last;
  return head;
}

似乎(通过试行和错误的行注释) next属性在当前指针似乎是问题,但我似乎不能明白为什么。 Node结构有两个属性,* next(指向链表中的下一个项)和数据(节点的数据)。

It seems (by trial and error of line commenting) that the access of the next attribute in the current pointer seems to be the problem, yet I can't seem to figure out why. The Node struct has two attributes, *next (which points to the next item in the linked list) and data (the data of the node).

linuxuser

linuxuser

编辑:问题解决了 - 非常感谢所有留下评论的人!

The problem was solved - thanks so much to all who left comments!

很遗憾,我无法使用 ** pHead 解除引用解决方案,因为问题出在网站自动输入函数的参数。然而,使用下面的注释,我做了一个简单的程序,我希望会详细说明这个问题对于像我这样的其他开始的C ++程序员:

Sadly, I wasn't able to use the **pHead dereferencing solution, as the problem is on a website that auto-inputs the arguments for the function. Using the comments below, however, I made a simple program that I hope will detail this issue for other beginning C++ programmers like me:

Node* Insert(Node *head,int data)
{
    if(head == NULL){
        Node* last = new Node();
        last -> data = data;
        return last;
    }

    Node *current = head;
    while(current -> next != NULL){
        current = current -> next;
    }
    Node *last = new Node();
    last -> data = data;
    last -> next = NULL;
    current -> next = last;
    return head;  
}

请注意,

linuxuser

linuxuser

推荐答案

这里最可能的问题是你不能使用 Insert 到跳转开始你的列表:如果 head NULL 开始,循环将失败马上。此外,在第一次插入时,您将无法分配

The most likely issue here is that you cannot use Insert to "jump start" your list: if the head is NULL to start with, the loop is going to fail right away. Moreover, on the first insertion you wouldn't be able to assign the head.

要解决此问题,将第一个参数从 Node * head 更改为节点** pHead ,传递一个指针到头指针,对插入函数的代码的额外级别:

To fix this problem, change the first parameter from Node *head to Node **pHead, pass a pointer to the head pointer, and add an extra level of dereference to the code of your Insert function:

Node* Insert(Node **pHead, int data)
{
    while(*pHead != NULL){
        pHead = &((*pHead)->next);
    }
    Node *last = new Node();
    last -> data = data;
    last -> next = NULL;
    *pHead = last;
    return last;
}

注意,这种方法将工作,即使你传递一个指针节点指针设置为 NULL

Note that this approach is going to work even if you pass a pointer to Node pointer that is set to NULL:

Node *head = NULL;
Insert(&head, 1);
Insert(&head, 2);
Insert(&head, 3);

这篇关于C ++结构指针Segfault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆