-3

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;
}
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • You might want to highlight the exact difference between the two. It takes a while to find. – Mateen Ulhaq Sep 23 '17 at 19:57
  • 2
    Don't use `malloc` in C++. `new` is almost always what you need. `unique_ptr` is even better. If you're not doing this for educational purposes, don't write your own container implementations; you're never going to match the std library's quality. `insert` should have been `Node::insert` instead. If you're not going to use `unique_ptr` then you have to manually clean-up after every dynamic allocation you make. – patatahooligan Sep 23 '17 at 20:01
  • @MateenUlhaq -- why have you deleted substantial portions of OP code, without comment? Now there is nothing close to a [MCVE], and the posted code looks like it could be C, instead of C++. The original code was clearly C++ despite OP tags. Rolling back. – ad absurdum Sep 23 '17 at 20:36

2 Answers2

3

With the code 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.

This is different from your second case, where struct Node* temp = (struct Node*) malloc(sizeof(struct Node)) allocates an object dynamically; the lifetime of such an object ends when it is deleted explicitly, such that you may then refer to its address.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

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

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

James Maa
  • 1,764
  • 10
  • 20