C 内存分配

C 内存分配,c,C,在insert_rear中,当函数调用getnode时,它会转到上面的函数,并创建一个节点,而在我使用gdb进行调试时,它会创建一个节点 node *getnode() { node *x; x = (node*)malloc(sizeof(node)); if (x==NULL) { printf("no memory \n"); ex

在insert_rear中,当函数调用getnode时,它会转到上面的函数,并创建一个节点,而在我使用gdb进行调试时,它会创建一个节点

node *getnode()
    {
        node *x;
        x = (node*)malloc(sizeof(node));
        if (x==NULL)
            {
                    printf("no memory \n");
                    exit(1);
            }
        return x;
    }


*insert_rear(int item ,node *first)
    {
        node  *temp;
        node  *cur;
        temp = getnode();


        temp -> data = item;
        temp -> next = NULL;

        if (first == NULL)
        return temp;

        cur = first;
        while(cur -> next != NULL)
          {
            cur = cur -> next;
          }

        cur -> next = temp;
        return first;
    }
这两者之间有什么区别。

(gdb) p temp
$7 = (struct classifier *) 0x8d8f080
(gdb) p &temp
$8 = (struct classifier **) 0xbff9cb04
您正在打印变量temp的堆栈地址。 与


您正在打印temp的值(这是getnode()返回的已分配内存的地址)

您可以有多个间接寻址:
temp
getnode()
接收到一个指针地址,该地址指向由
malloc
分配的节点内存

&temp
是存储此地址(指向节点)的内存地址

基本上你有

p temp

因此,如果您使用
*temp
,您就是在访问节点。如果您使用
*(&temp)
(不知道它的语法是否有效…)但是,您将访问存储节点存储地址的
temp

*&temp
是合法语法,就像
&*temp
一样。您是否考虑过参加编程课程,甚至聘请导师?我是认真的,因为您在很长一段时间内问了很多非常基本的问题。
p temp
&temp ---(points to)---> Memory X ***temp*** ---(points to)---> Memory Y ****MEMORY from malloc of "type" node****