Malloc memory circuit

Hi i have below code to create linked list

#include<stdio.h>
#include<stdlib.h>
struct node{
        unsigned int data1;
        unsigned int  data2;
        struct node *ptr;
}obj;
void enterData()                                                        // Here the EnterDAta fnnction uses the obj object to enter the data and note that this
{                                                                       // obj is used agauin and again in the every node of the list to enter the data
        printf("\n Enter the data1 ");
        scanf("%u",&obj.data1);
        printf("\n Enter the data2 ");
        scanf("%u",&obj.data2);
}
void append(struct node **start)                                        // This is used to append the dara un the list or also used to add the first element in the list
{
        enterData();
        struct node *next_node=*start;
        if(next_node==NULL)
        {
                printf("\nAdding first element in the list ......\n");
                next_node=malloc(sizeof(struct node));
                printf("\n The memory location of next_node is %p",&next_node);     
                if(next_node==NULL)
                {
                        printf("\n Out of Memory");
                }
                else{
                        next_node->data1=obj.data1;
                        printf("\n The memory location of next_node->data1 is %p",&next_node->data1);
                        next_node->data2=obj.data2;
                        printf("\n The memory location of next_node->data2 is %p",&next_node->data2);
                        next_node->ptr=NULL;
                        *start=next_node;                                               //This line of code here is modifying the header pointer see the magic of the pointer :)
                }
                printf("\n The first element added successfully");
        }
        else
        {
                printf("\n Appending the data ......\n");
                struct node *temp=next_node;
                next_node=malloc(sizeof(struct node));
                if(next_node==NULL)
                        printf("\n Out of Memory");
                else
                {
                        next_node->data1=obj.data1;
                        next_node->data2=obj.data2;
                        next_node->ptr=NULL;
                        while(temp->ptr!=NULL)
                                temp=temp->ptr;
                }
                temp->ptr=next_node;
                temp=NULL;
                printf("\n Data appended Successfully!!! ");

        }
next_node=NULL;
}
int main()
{
struct node *head=NULL;
append(&head);
return 0;
}

      

In the above code, tell me if I get the memory address next_node as 1000

, then the memory address I get for next_node->data1 is 1000

and the memory addressnext_node->data2 is 1004

But if in the above append function, if just change some changes in the code like this

void append(struct node **start)                                        // This is used to append the dara un the list or also used to add the first element in the list
{
        enterData();
        struct node *next_node=*start;
        if(next_node==NULL)
        {
                printf("\nAdding first element in the list ......\n");
                next_node=malloc(sizeof(struct node));
                if(next_node==NULL)
                {
                        printf("\n Out of Memory");
                }
                else{
                        next_node->data2=obj.data2;
            printf("\n The memory address of next_node->data2 is %p ",&next_node->data2);
                        next_node->data1=obj.data1;
            printf("\n The memory address of next_node->data1 is %p ",&next_node->data1);
                        next_node->ptr=NULL;
                        *start=next_node;                                               //This line of code here is modifying the header pointer see the magic of the pointer :)
                }
                printf("\n The first element added successfully");
        }
        else
        {
                printf("\n Appending the data ......\n");
                struct node *temp=next_node;
                next_node=malloc(sizeof(struct node));
                printf("\n The memory address of next_node is %p ",&next_node);
                if(next_node==NULL)
                        printf("\n Out of Memory");
                else
                {
                        next_node->data1=obj.data1;
                        next_node->data2=obj.data2;
                        next_node->ptr=NULL;
                        while(temp->ptr!=NULL)
                                temp=temp->ptr;
                }
                temp->ptr=next_node;
                temp=NULL;
                printf("\n Data appended Successfully!!! ");

        }

      

Now if the address next_node is 2000

then I get the memory address next_node->data1 as 2004

and for data2 is 2008

, butshouldn't it be the other way as we are first storing the data2 in the memory location using the next_node pointer ?

+3


source to share


1 answer


The relative addresses of the members of your nodes are a function of the layout struct node

, not the order in which you refer to them. If you change the members data1

and data2

in the ad struct node

, you'll see that data2

appears at the bottom address in each instance, but with the current ad data1

will appear first in each instance.



+4


source







All Articles