Thread: Printing address of structure pointer

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    32

    Printing address of structure pointer

    I have compiled my code codes but I do not understand what is happening from the output of the code


    Code:
    #include<stdio.h>
    
    #include<stdlib.h> 
         
    struct Node{
      int value;
      struct Node *next;
    };
    
    
    
    
    struct Node * Add_Node(  int data, struct Node *p ){
    
    
      struct Node *new = malloc(sizeof(*new));
      new-> value = data;
      new->next = p;
      printf("new : %p \n", new);
      return new;
    }
    
    
    int main ()
    {
    	 struct Node *Ps = NULL;
    	 printf("intial PS : %p \n", Ps);      
         Ps = Add_Node(12, NULL); 
    	 printf("first call PS : %p \n", Ps);
         Ps = Add_Node(24, Ps); 
    	 printf("Second call PS : %p \n", Ps); 
         Ps = Add_Node(34, Ps); 
    	 printf("Third call, PS : %p \n", Ps); 	 
    	 Ps = Add_Node(44, Ps); 
    	 printf("fourth call, PS : %p \n", Ps); 
         Ps = Add_Node(54, Ps); 
    	 printf("fifth call PS : %p \n", Ps);
    	 
    return 0;	 
    }
    intial PS : 00000000
    new : 007D13A8
    first call PS : 007D13A8
    new : 007D13D8
    Second call PS : 007D13D8
    new : 007D0DD8
    Third call, PS : 007D0DD8
    new : 007D0DE8
    fourth call, PS : 007D0DE8
    new : 007D0DF8
    fifth call PS : 007D0DF8

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,532
    Other than the memory leaks, what don't you understand?

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    32
    Quote Originally Posted by jimblumberg View Post
    Other than the memory leaks, what don't you understand?
    Code:
    structNode * Add_Node(  intdata, structNode *p ){
    
      struct Node *new = malloc(sizeof(*new));
      new-> value = data;
      new->next = p;
      printf("new : %p \n", new);
      return new;
    }
    intial PS : 00000000
    new : 007D13A8
    first call PS : 007D13A8
    new : 007D13D8
    Second call PS : 007D13D8
    new : 007D0DD8
    Third call, PS : 007D0DD8
    new : 007D0DE8
    fourth call, PS : 007D0DE8
    new : 007D0DF8
    fifth call PS : 007D0DF8
    first call :

    main function pass data 12 and pointer Ps. Ps is NULL pointer. Add_Node function take argument data and p. there is another pointer new. pointer new hold location 007D13A8 and point to first object of structure and then point to second object of structure

    This is my confession i want to know what happen in every function call

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,532
    What exactly don't you understand? You create a "new" node, add some data to that "new" node then return the pointer to that "new" node, overwriting the previous pointer creating a memory leak.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    716
    Quote Originally Posted by jimblumberg View Post
    What exactly don't you understand? You create a "new" node, add some data to that "new" node then return the pointer to that "new" node, overwriting the previous pointer creating a memory leak.
    Each node is linked to the previously-allocated node, making all nodes accessible, so there's no memory leak (except at the very end of the program because the nodes in the linked list are not freed).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2015, 07:43 AM
  2. Passing a structure by address
    By Sharke in forum C Programming
    Replies: 2
    Last Post: 03-24-2009, 12:43 AM
  3. Address Printing
    By Air in forum C Programming
    Replies: 17
    Last Post: 01-15-2009, 07:03 PM
  4. Printing out the memory address
    By Scalpel78 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2007, 11:01 AM
  5. printing memory at address..help!
    By myjay in forum C Programming
    Replies: 5
    Last Post: 09-21-2003, 03:58 PM

Tags for this Thread