简体   繁体   English

指针与指向结构错误的指针

[英]Pointer vs Pointer to Structure Bug

Here are both of My Codes.这是我的两个代码。 One Using structure and Another Using Pointer to Structure.一个使用结构,另一个使用指向结构的指针。 But when I am not using a Pointer it's not working.但是当我不使用指针时它不起作用。 Al though I think they are same.虽然我认为他们是一样的。 But I am still a beginner.但我仍然是初学者。 So I need to understand what's going wrong.所以我需要了解出了什么问题。

Not Working Code:不工作代码:

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

void insert(struct Node** head_ref,int data){
    //Not Working Here. The Header should change after every insert but it isn't Moving from it's Memory;

    struct Node temp ;

    temp.data = data;
    temp.next = (*head_ref);
    (*head_ref) = &temp;
}

int main(){
    struct Node *head = NULL;

    insert(&head,4);
    insert(&head,2);
    insert(&head,11);
    insert(&head,9);

            while(head->next !=0 ){
                std::cout << head->data <<" ";
                head = head->next;
            }
    return 0;
}

Working Code:工作代码:

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

void insert(struct Node** head_ref,int data){
    //The Problem is in This Line. Pointer to Structure is Working But only Structure isn't
    struct Node* temp = (struct Node*) malloc(sizeof(struct Node)) ;

    temp->data = data;

    temp->next = (*head_ref);
    (*head_ref) = temp;
}

int main(){
    struct Node *head = NULL;

    insert(&head,4);
    insert(&head,2);
    insert(&head,11);
    insert(&head,9);

            while(head->next !=0 ){
                std::cout << head->data <<" ";
                head = head->next;
            }
    return 0;
}

With the code struct Node temp ; ... (*head_ref) = &temp;使用代码struct Node temp ; ... (*head_ref) = &temp; struct Node temp ; ... (*head_ref) = &temp; , you store the address of a local variable. ,您存储局部变量的地址。 As soon as function insert has finished, the lifetime of the object stored in this variable ends, and accessing the pointer after this time is undefined behaviour.函数insert完成后,存储在此变量中的对象的生命周期结束,并且在此时间之后访问指针是未定义的行为。

This is different from your second case, where struct Node* temp = (struct Node*) malloc(sizeof(struct Node)) allocates an object dynamically;这与您的第二种情况不同,其中struct Node* temp = (struct Node*) malloc(sizeof(struct Node))动态分配一个对象; the lifetime of such an object ends when it is deleted explicitly, such that you may then refer to its address.当一个对象被显式删除时,它的生命周期就结束了,这样你就可以引用它的地址。

It is the difference between the heap and stack What and where are the stack and heap?它是堆和栈的区别栈和堆是什么以及在哪里?

void insert(struct Node** head_ref,int data){
    //Not Working Here. The Header should change after every insert but it isn't Moving from it's Memory;

    struct Node temp ;

    temp.data = data;
    temp.next = (*head_ref);
    (*head_ref) = &temp;
}

In the original code, temp is located on stack , since got destroyed automatically at the end of the scope在原始代码中, temp 位于stack ,因为在作用域结束时被自动销毁

void insert(struct Node** head_ref,int data){
    //The Problem is in This Line. Pointer to Structure is Working But only Structure isn't
    struct Node* temp = (struct Node*) malloc(sizeof(struct Node)) ;

    temp->data = data;

    temp->next = (*head_ref);
    (*head_ref) = temp;
}

This could work because it is located on heap , thus you have to manually delete it when you finish using it这可以工作,因为它位于heap ,因此您必须在使用完后手动删除它

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

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