c - Malloc 内存布局

标签 c pointers data-structures malloc

你好我有下面的代码来创建链表

#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;
}

在上面的代码中,如果我将 next_node 的内存地址设为 1000,那么我将为 next_node->data1 获取的内存地址为 1000 并且next_node->data2 的内存地址是 1004

但是如果在上面的追加函数中像这样调整代码中的一些变化

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!!! ");

        }

现在如果 next_node 的地址是 2000 那么我得到 next_node->data1 的内存地址为 2004data2 是 2008但是 不应该是另一种方式吗,因为我们首先使用 next_node 指针将 data2 存储在内存位置?

最佳答案

节点成员的相对地址是 struct node 布局的函数,而不是您访问它们的顺序。如果您在 struct node 的声明中交换 data1data2 成员,那么您将看到 data2 出现在每个实例中的较低地址,但使用当前声明,data1 将在每个实例中首先出现。

关于c - Malloc 内存布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30985562/

相关文章:

枚举可以被认为是不安全的吗?

c - 尝试为我的结构分配输入字符时,程序崩溃

c - 相同地址不同值

mysql - MySQL 中的多值属性

c - c中字符串的优先级队列

c++ - 用于随机访问和循环元素的最佳数据结构(C++)

c - 将 wave 加载到数组中 + 减去 channel + 另存为 wave/mp3

复制构造函数中的 C++ vector 数组

c++ - 'struct (*)[]' 和 'struct *[]' 有什么区别?

c++ - CMake的package_find冲突如何解决?