首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自指结构

自指结构
EN

Stack Overflow用户
提问于 2015-06-17 05:32:23
回答 1查看 854关注 0票数 4

自引用结构的定义方式如下:

代码语言:javascript
复制
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);
}

交货期是

结构中变量data1的地址是0xd0c7010。 结构中变量data2的地址是0xd0c7018。

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

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

代码语言:javascript
复制
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);
}

交货期是

结构中变量data1的地址为0xd0c6010。 结构中变量data2的地址为0xd0c6014。

所以data1占用了4个字节的内存,一个整数变量可以这样做,对吗?

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

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

代码语言:javascript
复制
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

对于第二个案子

代码语言:javascript
复制
 Address of variable data1 in struct is 0x600910
 Address of variable data2 in struct is 0x600914
 The size of struct is 8

我正在Linux上运行这段代码,使用gcc (GCC) 4.1.2

上面的代码工作得很好,但是下面的代码呢?

代码语言:javascript
复制
#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是

代码语言:javascript
复制
 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个字节的差异?我是在与其他两个环境相同的环境下运行这段代码的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-17 05:35:37

结构填充是您观察到的差异的原因。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30882980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档