Thread: Why pointer variable hold new memory at each function call

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    72

    Why pointer variable hold new memory at each function call

    Why pointer variable (new_node) hold new memory at each function call in my code ?

    Code:
      #include<stdio.h>
    #include<stdlib.h>
    
    struct node
    {
        struct node *next_node;
    };
    
    
    struct node *addNodeToFront( struct node *list)
    {
        struct node *new_node = malloc (sizeof(*new_node)); 
        printf("new_node = %p\n", (void *)new_node);
        if ( new_node != NULL)
        {
          //  new_node -> next_node = list;
            
        }
        return  new_node;
    }
    
    
       
    int main()
    {
       
        struct node *list = NULL;
        list = addNodeToFront(list);
        list = addNodeToFront(list);
        list = addNodeToFront(list);
        return 0;   
    }
    new_node = 00021230
    new_node = 00021260
    new_node = 00021270

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,131
    Because you are adding a new node to the front of the list each time.

    Edit: In your case you are only allocation space for a new node each time.
    I suggest reading the assignment.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    72
    Quote Originally Posted by stahta01 View Post
    Because you are adding a new node to the front of the list each time.

    Edit: In your case you are only allocation space for a new node each time.
    I suggest reading the assignment.

    Tim S.
    I don't understand clearly why new memory allocate each time

    malloc return pointer in function

    Code:
     #include<stdio.h>
    #include<stdlib.h>
    
    struct node
    {
        struct node *next_node;
    };
    
    
    struct node *addNodeToFront( struct node *list)
    {
        struct node *new_node = malloc (sizeof(*new_node)); 
        printf("new_node = %p\n", (void *)new_node);
        if ( new_node != NULL)
        {
           new_node -> next_node = list;
           printf("new_node -> next_node = %p\n", (void *) new_node -> next_node);    
        }
        return  new_node;
    }
    
    
       
    int main()
    {
       
        struct node *list = NULL;
        printf("list = %p\n", (void *)list);
        list = addNodeToFront(list);
        printf("list = %p\n", (void *)list);
        list = addNodeToFront(list);
        list = addNodeToFront(list);
        return 0;   
    }
    list = 00000000
    new_node = 00B61230
    new_node -> next_node = 00000000
    list = 00B61230
    new_node = 00B61260
    new_node -> next_node = 00B61230
    new_node = 00B61270
    new_node -> next_node = 00B61260

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,122
    > I don't understand clearly why new memory allocate each time
    Because that's what malloc does, returns a pointer to some new memory.

    If you want to store a list of 10 items, you need to allocate space for 10 items somehow.

    In this case, this means creating a list of 10 nodes, which will entail calling malloc (via addNodeToFront) 10 times.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2022
    Posts
    72
    Quote Originally Posted by Salem View Post
    > I don't understand clearly why new memory allocate each time
    Because that's what malloc does, returns a pointer to some new memory.

    If you want to store a list of 10 items, you need to allocate space for 10 items somehow.

    In this case, this means creating a list of 10 nodes, which will entail calling malloc (via addNodeToFront) 10 times.
    Code:
     #include<stdio.h>
    #include<stdlib.h>
    
    
    struct node
    {
        int data;
        struct node *next_node;
    };
    
    
    struct node *addNodeToFront( struct node *list, int value)
    {
        struct node *new_node = malloc (sizeof(*new_node)); 
    
    
        if ( new_node != NULL)
        {
           new_node -> data = value;    
           new_node -> next_node = list;
           
        }
        return  new_node;
    }
    
    
    void show(struct node *c)
    {
        if (c == NULL)
            return;
        show(c -> next_node);
        printf("%d\n", c-> data);
    } 
       
    int main()
    {
       
        struct node *list = NULL;
        list = addNodeToFront(list, 1);
    
    
        show(list);
        return 0;   
    }
    I know program has thses variables
    list ( pointer variable type struct )
    value ( variable type int)
    new_node ( pointer variable type struct )
    c( pointer variable type struct )

    Are these variable in code ?


    new_node -> data
    new_node -> next_node

    I think they are variables and occupy memory. They will store the data of node at function call

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initialize a variable with a function call
    By jvh24521 in forum C Programming
    Replies: 6
    Last Post: 08-23-2012, 10:33 AM
  2. Variable shows NAN after return from function call
    By CodeKate in forum C Programming
    Replies: 21
    Last Post: 11-18-2010, 11:58 AM
  3. Can't hold numbers in variable.
    By xamlit in forum C Programming
    Replies: 10
    Last Post: 12-02-2005, 05:05 PM
  4. I have the Memory Address of a function, how do I call it?
    By vulcan_146 in forum C++ Programming
    Replies: 8
    Last Post: 05-22-2005, 02:00 AM
  5. call function from variable
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 11-29-2003, 05:40 AM

Tags for this Thread