c - 自指结构

标签 c pointers struct

自引用结构定义如下:

struct node{
    int data1;
    int data2;
    struct node *ptr;
}obj,*temp;

int main()
{
     printf("\n Address of variable data1 in struct is %p",&obj.data1);
     printf("\n Address of variable data2 in struct is %p",&obj.data2);
}

O/P 是

Address of variable data1 in struct is 0xd0c7010
Address of variable data2 in struct is 0xd0c7018

这意味着 data1 占用了 8 个字节的内存,对吧?

但是如果我有如下结构定义

struct node{
    int data1;
    int data2;
}obj,*temp;

int main()
{
     printf("\n Address of variable data1 in struct is %p",&obj.data1);
     printf("\n Address of variable data2 in struct is %p",&obj.data2);
}

O/P 是

Address of variable data1 in struct is 0xd0c6010
Address of variable data2 in struct is 0xd0c6014

所以 data1 占用了整型变量占用的 4 个字节的内存,对吧?

但是为什么第一种情况data1占用的内存空间增加了呢?

编辑: 对于第一种情况 o/p 是

Address of variable data1 in struct is 0x600940
Address of variable data2 in struct is 0x600944
The address of ptr is 0x600948
The size of struct is 16

对于第二种情况

 Address of variable data1 in struct is 0x600910
 Address of variable data2 in struct is 0x600914
 The size of struct is 8

我在 Linux 上运行这段代码使用 海合会 (海湾合作委员会) 4.1.2

上面的代码工作正常,但是下面这个呢

#include <stdio.h>
#include <stdlib.h>

struct node
{
  int data;
  struct node *next;
};

// This function prints contents of linked list starting from the given node
void printList(struct node *n)
{
  printf("\n The memory location of n is %p ",n);
  while (n != NULL)
  {
     printf(" %d ", n->data);
     n = n->next;
     printf("\n The memory location of n in while loop is %p ",n);
  }
}

int main()
{
  struct node* head = NULL;
  struct node* second = NULL;
  struct node* third = NULL;

  // allocate 3 nodes in the heap
  head  = (struct node*)malloc(sizeof(struct node));
  second = (struct node*)malloc(sizeof(struct node));
  third  = (struct node*)malloc(sizeof(struct node));

  head->data = 1; //assign data in first node
  head->next = second; // Link first node with the second node
  printf("\n The memory address of head->data is %p ",&head->data);
  printf("\n The memory address of head->next is %p ",&head->next);

  second->data = 2; //assign data to second node
  second->next = third;
  printf("\n The memory address of second->data is %p ",&second->data);
  printf("\n The memory address of second->next is %p ",&second->next);

  third->data = 3; //assign data to third node
  third->next = NULL;

  printf("\n The memory address of third->data is %p ",&third->data);
  printf("\n The memory address of third->next is %p ",&third->next);

  printList(head);

  getchar();
  printf("\n");
  return 0;
}

O/P 是

 The memory address of head->data is 0x215c010
 The memory address of head->next is 0x215c018
 The memory address of second->data is 0x215c030
 The memory address of second->next is 0x215c038
 The memory address of third->data is 0x215c050
 The memory address of third->next is 0x215c058
 The memory location of n is 0x215c010  1
 The memory location of n in while loop is 0x215c030  2
 The memory location of n in while loop is 0x215c050  3
 The memory location of n in while loop is (nil)

为什么现在还相差8个字节? 我在与其他两个相同的环境下运行此代码。

最佳答案

Struct padding是您观察到差异的原因。

我认为您使用的是 64 位系统,其中指针大小为 8 个字节(在带有指针的结构中)。所以编译器将所有三个成员对齐到 8 字节对齐。但在后一种情况下,它只有两个 int,所以它对齐到 4 个字节。

关于c - 自指结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30882980/

相关文章:

c - 嵌入 Lua 5.2 和定义库

c++ - 分配给函数调用返回的指针

c++ - 运算符<<在单例中的重新定义

可区分联合的结构属性

为包含动态数组的结构创建 MPI 类型

c - C 中的结构体初始化

c - 在c字符串中输入多个单词

c - 预期声明说明符或 '...' 之前的 'compareWeight'

c - 如何将整数存储在 char* 指向的位置

c - 如何将主机名转换为 DNS 名称?