Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中函数返回的指针_C_Pointers_Linked List_Segmentation Fault_Return - Fatal编程技术网

C中函数返回的指针

C中函数返回的指针,c,pointers,linked-list,segmentation-fault,return,C,Pointers,Linked List,Segmentation Fault,Return,下面是制作具有2个指针的链表的代码。链接列表正在创建和打印,所有指针prev+next都正常。但是,当我调用函数copay并将其valuepointer赋值为duplicate时,我得到了分段错误,但是如果我只使用copay而不将其赋值给任何其他变量,那么就没有问题了 typedef struct node { int data; struct node *next; struct node *prev; } node; void insert(node **head

下面是制作具有2个指针的链表的代码。链接列表正在创建和打印,所有指针prev+next都正常。但是,当我调用函数copay并将其valuepointer赋值为duplicate时,我得到了分段错误,但是如果我只使用copay而不将其赋值给任何其他变量,那么就没有问题了

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


void insert(node **head, int data) {
    node *new = (struct node *)malloc(sizeof(node));
    new->data = data;
    new->next = NULL;
    node *temp = *head;
    if (!(temp)) {
        *head = new;
        new->prev = NULL;
        // printf("\n return  : %d",data);
        return;
    }

    while (temp->next)
        temp = temp->next;

    temp->next = new;
    new->prev = temp;
    // printf("\n return  : %d",data);
}

void print(node **head) {
    node *temp = *head;
    printf("\n");
    while (temp) {
        printf(" %d ->", temp->data);
        temp = temp->next;
    }
    printf(" NULL\n");
}

node *copay(node **head) {
    node *temp = *head;
    return temp;
}

int main() {
    node *head;

    insert(&head, 1);
    insert(&head, 3);
    insert(&head, 5);
    insert(&head, 7);
    insert(&head, 9);
    (head)->prev = (head)->next->next;
    (head)->next->next->prev = (head)->next->next->next->next;
    (head)->next->next->next->next->prev = (head)->next;


    print(&head);
    node *duplicate = copay(&head);

    // print(&duplicate);
}

copay在运行时实际上很好。这个问题发生在它的调用者身上。在copay退出后,它是主函数。copay返回指向节点的指针,但问题是只有在copay运行时才分配本地节点temp。当copay退出时,其所有本地服务器都将被解除分配。因此,调用者只剩下一个指向解除分配节点的指针

我建议你们阅读斯坦福CS教育图书馆的第2部分,这是关于本地内存的


来源:

函数main中有一个非常简单的问题:

head已定义但未初始化。您必须将其初始化为NULL,insert才能正常工作,否则您将有未定义的行为。顺便说一句,将实际将节点附加到列表中的函数命名为insert是令人困惑的。将此行更改为:

node *head = NULL;
我不理解您试图通过以下几行来实现:

(head)->prev = (head)->next->next;
(head)->next->next->prev = (head)->next->next->next->next;
(head)->next->next->next->next->prev = (head)->next;

其余的我看还可以。

这里没有问题。看起来像是某种隐式请求,用于为您调试程序。不如你自己先试试,如果你还是找不到问题,那就把你的发现详细公布出来。不要把你的代码扔给我们,这不是免费的找我的bug服务!在将头部传递给insert之前,尚未初始化头部。所有赌注都输光了;任何事情都有可能发生。在第一次调用insert之前,您可能应该将其设置为0 NULL—最好是作为初始化而不是赋值。Jonathan:insert运行正常。唯一的问题是最后一行duplicate=copay。如果我使用copay,然后我没有得到任何错误。temp中的值,*head的副本是不确定的,因为head未初始化。您不知道是执行if子句还是执行函数的其余部分。由于您没有显示任何地址打印,Anuj,我不清楚您如何知道insert功能正常。您是否已使用运行代码?如果不是,如果是一个选项,那么你应该这样做…并学习如何调试。这是完全不正确的。实际上,当我引用给定源的本地内存部分时,从copaya返回局部变量的值是完全正确的,它通过示例解释说,将指向局部变量的指针返回到调用方函数会导致错误,因为它在函数终止后被释放。它实际上显示在“符号与错误”小节中。如果我误解了这个部分,请你帮我弄清楚。copay函数返回一个本地指针temp的值。此指针不指向局部变量,它是传递其地址的指针的副本。从上下文来看,它要么为NULL,要么指向malloc分配的对象。函数main中有一个bug,因为head未初始化,但您所解释的内容与此无关。
(head)->prev = (head)->next->next;
(head)->next->next->prev = (head)->next->next->next->next;
(head)->next->next->next->next->prev = (head)->next;