简体   繁体   English

在嵌入结构本身还是在结构内部嵌入指向结构的指针之间是否有选择原则?

[英]Is there a principle for choosing between embedding a struct itself or the pointer to a struct inside a struct?

This is a code snippet from qemu.(qemu-5.1.0 include/hw/arm/smmu-common.h)这是来自 qemu 的代码片段。(qemu-5.1.0 include/hw/arm/smmu-common.h)

typedef struct SMMUDevice {
    void               *smmu;
    PCIBus             *bus;
    int                devfn;
    IOMMUMemoryRegion  iommu;
    AddressSpace       as;
    uint32_t           cfg_cache_hits;
    uint32_t           cfg_cache_misses;
    QLIST_ENTRY(SMMUDevice) next;
} SMMUDevice;

I've seen many such codes until now but I am now curious if there is any principle/rule in choosing between到目前为止,我已经看过很多这样的代码,但我现在很好奇在两者之间进行选择时是否有任何原则/规则

  1. embedding a struct A inside a struct B将结构 A 嵌入结构 B
  2. embedding a pointer to the struct A inside a struct B在结构 B 中嵌入指向结构 A 的指针

Two things that come to my mind right away is that if a struct A is to be shared by many structs, it is better to use pointer.我马上想到的两件事是,如果结构 A 要由许多结构共享,最好使用指针。 or if the struct containing the struct(that is, struct B) is to be frequently passed as a function argument, it would be better to use pointer(pointer to struct B as argument, or pointer to A inside struct B and struct B is the argument) because copying the struct to stack would take long time.或者,如果包含结构(即结构 B)的结构要经常作为 function 参数传递,则最好使用指针(指向结构 B 的指针作为参数,或指向结构 B 内部的 A 和结构 B 的指针是参数),因为将结构复制到堆栈需要很长时间。

I am curious if there are other important rules.我很好奇是否还有其他重要规则。

There's no correct answer because it depends on what you want to use them for.没有正确答案,因为这取决于你想用它们做什么。 Storing a struct inside another struct is generally more efficient, since it gives faster access and better data cache use.将结构存储在另一个结构中通常效率更高,因为它提供更快的访问和更好的数据缓存使用。

However, it isn't as flexible.但是,它没有那么灵活。 If you wish to swap out the whole contents of a big struct for something else, it goes much faster to just swap two pointers than doing a hard copy of all the data.如果您希望将一个大结构的全部内容换成其他内容,只交换两个指针比对所有数据进行硬拷贝要快得多。 Pointers also enable different forms of allocation - you could have a static storage struct with a pointer at dynamically allocated memory for example.指针还启用不同的 forms 分配 - 例如,您可以拥有一个 static 存储结构,其中指针位于动态分配的 memory。


if a struct A is to be shared by many structs, it is better to use pointer如果一个结构 A 要被许多结构共享,最好使用指针

I don't see how that matters at all.我根本不明白这有什么关系。 It's just a .这只是一个. vs -> notation by the code using it. vs ->使用它的代码表示法。

or if the struct containing the struct(that is, struct B) is to be frequently passed as a function argument, it would be better to use pointer或者如果包含结构(即结构 B)的结构要经常作为 function 参数传递,最好使用指针

No that's nonsense, you'd always pass the outer struct through a pointer no matter what members it got.不,那是废话,无论它有什么成员,你总是通过指针传递外部结构。 Passing it by value doesn't make any sense in either scenario.在任何一种情况下,按值传递它都没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM