如何使用C编程语言实际实现结构? [英] How are structs actually implemented in the C programming language?

查看:92
本文介绍了如何使用C编程语言实际实现结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结构是C编程语言中的复合数据结构;它们由诸如整数和指针之类的原语组成,它们全部以相邻的方式(如数组)放置在内存中.

Structs are a composite data structure in the C programming language; they consist of primitives such as ints and pointers all placed in memory in an adjacent fashion, such as an array.

我的问题是,结构本身是由什么制成的?它们是数组的类型吗?例如,哈希表可以实现为链接列表的数组.以类似的方式,将结构实现为什么? 如果需要,请在x86汇编程序级别进行解释.谢谢.

My question is, what are structs themselves made out of? Are they a type of array? For example, a hash table can be implemented as an array of linked-lists. In a similar way, what is a struct implemented as? If need be, please explain at the x86 assembly level. Thank you.

推荐答案

在程序集级别,结构归结为一个由对应于结构成员的偏移量访问的地址.

At assembly level Structure boils down to a address accessed by offset corresponding to structure member.

根据对齐规则,为结构实例分配存储类内存.

Depending on the alignment rule and storage class memory is allocated for instance of structure.

示例:

struct A
{
  int a,
  char b
}a1;

在上述情况下,如果您编写a1.b = 5,则它的程序集等效项将是:

In above case if you write a1.b = 5 its assembly equivalent would be something:

MOV 5 TO ADDRESS OF a1 + 4//假定整数大小为4

MOV 5 TO ADDRESS OF a1 + 4 //assuming integer size is 4

这篇关于如何使用C编程语言实际实现结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆