初始化结构指针给出分段故障

发布于 2025-01-29 14:43:22 字数 980 浏览 2 评论 0原文

我一直在尝试在for循环中初始化链接列表。每次迭代,我都会创建一个指向节点结构的指针,并指向列表中的最后一个节点。但是,发生的奇怪的事情是,当试图将结构的数据字段的值分配给5(for循环中的第二行)时,我一直会遇到细分故障。不知道为什么会发生这种情况。

struct node{
  int data;
  node* next;
};

void CreateLinkedList(node* start, int numberOfNodes)
{
  int i = 0;
  node* tempo = start;

  for(i=0; i<numberOfNodes; i++){
      node* newNode;
      newNode->data = 5;
      newNode->next = NULL;
      while(tempo->next != NULL){
        tempo = tempo->next;
      }
      tempo->next = newNode;
  }
}

我尝试过的其他事情是使用“新”操作员,它起作用(再次不确定为什么):

void CreateLinkedList(node* start, int numberOfNodes)
{
  int i = 0;
  node* tempo = start;
  node* newNode;

  for(i=0; i<numberOfNodes; i++){
      newNode = new node;
      newNode->data = 5;
      newNode->next = NULL;
      while(tempo->next != NULL){
        tempo = tempo->next;
      }
      tempo->next = newNode;
  }
}

有什么想法吗?

PS:这是我在这里的第一篇文章!

I've been trying to initialise a linked list in a for loop. Every iteration, I create a pointer to a node struct and point the last node in the list to it. However, something strange that happens is I keep getting a segmentation fault when trying to assign the value of the data field of the struct to 5 (second line in the for loop). Not sure why this is happening.

struct node{
  int data;
  node* next;
};

void CreateLinkedList(node* start, int numberOfNodes)
{
  int i = 0;
  node* tempo = start;

  for(i=0; i<numberOfNodes; i++){
      node* newNode;
      newNode->data = 5;
      newNode->next = NULL;
      while(tempo->next != NULL){
        tempo = tempo->next;
      }
      tempo->next = newNode;
  }
}

Something else I tried was using the "new" operator and it worked (again, not sure why):

void CreateLinkedList(node* start, int numberOfNodes)
{
  int i = 0;
  node* tempo = start;
  node* newNode;

  for(i=0; i<numberOfNodes; i++){
      newNode = new node;
      newNode->data = 5;
      newNode->next = NULL;
      while(tempo->next != NULL){
        tempo = tempo->next;
      }
      tempo->next = newNode;
  }
}

Any ideas?

PS: This is my very first post here!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

站稳脚跟 2025-02-05 14:43:22

两个功能都是不正确的。在第一个函数中,您至少使用具有不确定值的非初始化指针newNode来访问内存,

for(i=0; i<numberOfNodes; i++){
    node* newNode;
    newNode->data = 5;
    newNode->next = NULL;
    //.. 

即使在第二个函数中使用新运算符

for(i=0; i<numberOfNodes; i++){
    newNode = new node;
    newNode->data = 5;
    newNode->next = NULL;
    //...

也不会使该函数正确。

用户可以将零指针传递给该功能(当列表为空时)。在这种情况下,您遇到了使用空指针访问内存的问题,

while(tempo->next != NULL){
      ^^^^^^^^^^ 

可以声明并定义以下方式

void CreateLinkedList( node * &start, size_t numberOfNodes )
{
    node **current = &start;

    while (numberOfNodes--)
    {
        *current = new node{ 5, nullptr };
        current = &( *current )->next;
    }
}

The both functions are incorrect. In the first function you are using at least the uninitialized pointer newNode with an indeterminate value to access memory that invokes undefined behavior

for(i=0; i<numberOfNodes; i++){
    node* newNode;
    newNode->data = 5;
    newNode->next = NULL;
    //.. 

Nevertheless even using the operator new in the second function

for(i=0; i<numberOfNodes; i++){
    newNode = new node;
    newNode->data = 5;
    newNode->next = NULL;
    //...

does not make the function correct.

The user can pass a null pointer to the function (when the list is empty). In this case you have the same problem of accessing memory using a null pointer

while(tempo->next != NULL){
      ^^^^^^^^^^ 

The function can be declared and defined the following way

void CreateLinkedList( node * &start, size_t numberOfNodes )
{
    node **current = &start;

    while (numberOfNodes--)
    {
        *current = new node{ 5, nullptr };
        current = &( *current )->next;
    }
}
任谁 2025-02-05 14:43:22

我同意弗拉德。首先,当您应该在构造结构之后设置数据时,您只是在创建该结构的新指针。这意味着您的内存尚未分配,您正在尝试进入不可访问的内存区域。为了解决您需要创建某些构造函数(或使用默认值),然后使用新的 +构造函数创建对象。可以是一些幼稚的版本。

    struct node{
      int data;
      node* next;
      
      node()
      {
       data = 0;
       node* = nullptr;
      }
   };
      //.
      //.
      //.
   for(i=0; i<numberOfNodes; i++){
      node* new node();
      //.
      //.
      //.

另外,正如弗拉德(Vlad)告诉您的那样,请注意该段条款很重要,因为该功能可以收到一些无效的价值,因此也要考虑一下。

I'm agree with Vlad. First of all when you are supposed to set data after construct the struct, and you just are creating a new pointer of that struct. that means that your memory is not already allocated and you are trying to access into a non-accessible region of memory. In order to solve that you need to create some constructor (or use the default), and then create the object with the new + the constructor. Some naïve version can be.

    struct node{
      int data;
      node* next;
      
      node()
      {
       data = 0;
       node* = nullptr;
      }
   };
      //.
      //.
      //.
   for(i=0; i<numberOfNodes; i++){
      node* new node();
      //.
      //.
      //.

Also as Vlad told you it's important to take care of the while clause since the function can receive some null value, so take care of that too.

不甘平庸 2025-02-05 14:43:22

C ++具有初始化结构的构造函数,因此它们从不处于某种不确定的状态。所以使用它们。

struct Node {
    int data{};
    Node* next{nullptr};

    Node(Node **head, int data_) : data{data_} {
        while(*head != nullptr) head = &(*head)->next;
        *head = this;
    }
};

然后,这简化了功能:

Node * CreateLinkedList1(int numberOfNodes) {
    Node *head{nullptr};
    for(int i=0; i < numberOfNodes; i++) {
        new Node(&head, 5);
    }
    return head;
}

记住列表的尾巴以便您可以直接附加到它会更快。

Node * CreateLinkedList2(int numberOfNodes) {
    Node *head{nullptr};
    Node **tail{head};
    for(int i=0; i < numberOfNodes; i++) {
        tail = &(new Node(tail, 5))->next;
    }
    return head;
}

C++ has constructors to initialize structures so they are never in some undefined state. So use them.

struct Node {
    int data{};
    Node* next{nullptr};

    Node(Node **head, int data_) : data{data_} {
        while(*head != nullptr) head = &(*head)->next;
        *head = this;
    }
};

This then simplifies the function:

Node * CreateLinkedList1(int numberOfNodes) {
    Node *head{nullptr};
    for(int i=0; i < numberOfNodes; i++) {
        new Node(&head, 5);
    }
    return head;
}

It would be much faster though to remember the tail of the list so you could append to it directly.

Node * CreateLinkedList2(int numberOfNodes) {
    Node *head{nullptr};
    Node **tail{head};
    for(int i=0; i < numberOfNodes; i++) {
        tail = &(new Node(tail, 5))->next;
    }
    return head;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文