0

I am new to C. Is there different memory management in both cases like the first one stores data in the stack and the second one in heap. In the first case, is the memory allocated by the compiler. Which one is better to use?

FIRST CODE

#include <stdio.h>
#include <stdlib.h>

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

void printLL(struct Node* head){
    printf("%d ", (*head).data);
    if((*head).next!=NULL){
        printLL((*head).next);
    }
}

int main()
{
    struct Node first, second, third;
    
    first.data = 9;
    first.next = &second;
    
    second.data = 3;
    second.next = &third;
    
    third.data = 7;
    third.next = NULL;
    
    printLL(&first);
    
    return 0;
}

SECOND CODE

#include <stdio.h>
#include <stdlib.h>

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

void printLL(struct Node* head){
    printf("%d ", (*head).data);
    if((*head).next!=NULL){
        printLL((*head).next);
    }
}

int main()
{
    struct Node* first;
    struct Node* second;
    struct Node* third;
    
    first = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));
    
    first->data = 9;
    first->next = second;
    
    second->data = 3;
    second->next = third;
    
    third->data = 7;
    third->next = NULL;
    
    printLL(first);
    
    return 0;
}

if there are any resources to read from pls send the link

Evg
  • 25,259
  • 5
  • 41
  • 83
  • 2
    The main difference is the [lifetime](https://en.cppreference.com/w/c/language/lifetime) of the nodes. In the first case, their lifetime ends when they go out of [scope](https://en.cppreference.com/w/cpp/language/scope). In the second case, their lifetime ends when you call [`free`](https://en.cppreference.com/w/c/memory/free). – Andreas Wenzel Jul 20 '21 at 02:46
  • AFAIK; normal variables on stack while in malloc memory on heap and pointer on stack. I have no idea which one is faster though. – Aval Sarri Jul 20 '21 at 07:41
  • 1
    @AvalSarri the [stack is faster](https://stackoverflow.com/a/161061/10934377), much [faster](https://stackoverflow.com/a/80113/10934377). – Heiko Ribberink Apr 10 '22 at 19:31

1 Answers1

0

It seems that you have missed the whole point of dynamic memory data structures, of which the linked list is the one usually taught first.
By bad luck your two MREs ( [mre] ) effectively hide that benefit.

In order to see the difference yourself, please attempt to add another node to your linked lists in both cases. Note that it does not need to be accessable directly, it is sufficient if you can access it from the previous node (otherwise the concept is prevented).

Then do that 100 times more.

Details on the concept and (artificial) use cases you will find in any class material, text book or tutorial on "linked lists". I intentionally do not provide a solution here, because an elaborate example/exercise which fits smoothly into your other learning progress is more useful to you.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54