繁体   English   中英

是否有必要使用“新”将新的指针分配给与先前存在的指向同一结构的指针相同的结构? 如果是这样,为什么?

[英]Is it necessary to use 'new' to allocate a new pointer to a struct identical to a previously existing pointer to the same struct? If so, why?

我正在写一个函数,将一个新元素插入一个单链表中。

该函数接受列表的* head,将要放置在新元素中的数据和l-list中的位置插入。

如果我不太清楚, 是编程练习的链接。

以下代码可以完美运行-

/*
  Insert Node at a given position in a linked list 
  head can be NULL 
  First element in the linked list is at position 0
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/

Node* InsertNth(Node *head, int data, int position)
{
    //Takes head of list as input and returns it after inserting a new element
    //with `data` at `position` in the l-list

    Node *temp = new Node;
    temp = head;
    Node *newn = new Node;
    newn->data = data;

    if(position == 0){
        newn->next = head;
        return newn;
    }

    while(--position){
        temp = temp->next;
    }
    newn->next = temp->next;
    temp->next = newn;

    return head;
}

但是,我不明白为什么必须使用Node *temp = new Node; temp = head; Node *temp = new Node; temp = head;

为什么不简单地使用Node *temp = head; 工作? 我仅使用temp指针遍历l-list,以便在返回最终列表时可以保留头部的位置。

编辑-我知道Node *temp = head; 是要走的路。 这是我最初是如何编程的这些话,但我忘了说,这是什么给了我一个段错误 当我将其更改为Node *temp = new Node; temp = head; Node *temp = new Node; temp = head; 它适用于所有测试用例(包括head为NULL的那些用例)。

我想知道的是,为什么这种看似荒谬的内存分配似乎必须使它起作用。

您上面发布的代码泄漏。

Node *temp = new Node; temp = head;

不好

Node *temp = head;

这个更好。

您的代码中还有其他问题。 但是您的分析认为对新方法很愚蠢,然后立即重新分配指针是正确的 发现得好。

在我之前发布的答案都是错误的,请确保它们指出您的代码泄漏,但是他们没有检查其余的代码以查看其是否真正按照预期的方式工作。 您的代码很容易出错,因为您没有说明head为NULL,这是明确说明的。

// Returns the new node inserted at the given Position inside the l-list
Node* InsertNth(Node *head, int data, int position)
{
    Node *newn = new Node;
    newn->data = data;
    newn->next = 0;

    // No head, return node right away
    if(!head)
    {
        return newn;
    }
    // Exception - only case where head is not returned
    else if(!position)
    {
        newn->next = head;
        return newn;
    }

    // Create ´temp´ which is a pointer to the next node in the list
    Node *temp = head;
    // The function allows passing of a signed int, make sure we stay above 0
    // as the previously while(--position) would go into an endless loop
    // if a signed integer were passed on to the function
    while(--position > 0)
    {
        // Just incase the input is bad, and position exceeds the size of the list
        if(!temp->next)
        {
            break;
        }

        temp = temp->next;
    }

    // Now that we found the place in line, insert it between the temp and the next item
    // in the list. Which may also be NULL
    newn->next = temp->next;
    temp->next = newn;
    return head;
}

现在您可以看到已经进行了一些更改,前面的代码没有说明head为NULL,并且没有正确地将成员“ next”设置为0,如果有人反复访问,最终将导致崩溃。假定以空值结尾的单链接列表。

用于

Node *temp = new Node;
temp = head;

导致内存泄漏。 第一行分配的内存丢失。 您需要使用:

Node *temp = head;

暂无
暂无

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

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