简体   繁体   English

指针与结构声明中的局部变量

[英]Pointers vs. local variables in struct declaration

I am often facing this dilemma: 我经常面临这样的困境:

typedef struct {
    char *name;
    int est;
    list *employees;
} company;

vs.

typedef struct {
    char *name;
    int est;
    list employees; // here I used local variable instead of a pointer
} company;

Which are the general concerns that come into play when choosing one over the other? 选择其中一个时,哪些是普遍关注的问题?

As far as I can tell, omitting the pointer saves a little memory, whereas keeping the pointer can be useful when the same referenced memory is used by several structs (which is not the case here anyway). 据我所知,省略指针会节省一点内存,而当多个结构使用相同的引用内存时,保持指针会很有用(这里不是这种情况)。 Also, when I use the local variable definition, the variable is given memory, in contrast to the use of a pointer, which requires me to malloc the memory myself. 此外,当我使用局部变量定义时,变量被赋予内存,与使用指针相反,这需要我自己对内存进行malloc

Is there anything else that should be taken into account? 还有什么需要考虑的吗?

Edit: list is a single-linked list: 编辑: list是单链表:

typedef struct {
    list_node *head, *tail;
} list;

My question is not limited to this particular case, though, I would like to know what shall be considered in general when choosing one over the other. 我的问题并不局限于这种特殊情况下,不过,我想知道是什么,一般应当选择一个比其他时候要考虑。

Memory management. 内存管理。 If you make the list instance part of the struct, it's allocated/freed with the struct. 如果将列表实例作为结构的一部分,则使用结构分配/释放它。 If you point to it, you allocate/free it separately. 如果你指向它,你可以单独分配/释放它。 If it might be pointed to from multiple places and/or have a lifespan not 100% coincident with the struct, managing it separately and pointing to it has obvious advantages. 如果它可能从多个地方指向和/或寿命与结构不是100%重合,则单独管理它并指向它具有明显的优点。

Basically, this is a judgement call based on how you expect the data to be used. 基本上,这是基于您期望数据使用方式的判断调用。

 list employees; // here I used local variable instead of a pointer`

Is is not a local variable. 是不是局部变量。

Just another member of the structure that happens not to be a pointer. 只是结构中的另一个成员,它不是指针。

It is the complete contents of list so you do not need to allocate that memory 它是list的完整内容,因此您无需分配该内存

If a structure contains a pointer, then a struct assignment will cause the destination structure to include a pointer to the same item as the pointer in the original. 如果结构包含指针,则结构赋值将使目标结构包含指向与原始指针相同的项的指针。 If the structure contains an array or a nested structure, the destination structure will receive its own copy of the array or nested structure. 如果结构包含数组或嵌套结构,则目标结构将接收其自己的数组副本或嵌套结构。 It will generally be easier to reason about structures which encapsulate all their data directly, though if the structure contains a linked list such encapsulation will likely not be possible in any case. 通常更容易推理直接封装其所有数据的结构,但如果结构包含链表,则在任何情况下都可能无法进行封装。 Given that, the fundamental question is what you want employees to encapsulate. 鉴于此,基本问题是您希望employees封装的内容。

  • If you want it to encapsulate a view of a list which is never going to change as long as the structure holds the pointer and remains in use, employees may either hold a list or a *list . 如果您希望它封装一个永远不会改变的列表视图,只要该结构保持指针并保持使用, employees可以持有list*list

  • If you want it to encapsulate a view of a list which is owned by some other code, and whose head and tail might change, then employees should hold a *list . 如果您希望它封装一个列表的视图,该列表由一些其他代码拥有,并且其headtail可能会发生变化,那么employees应该拥有一个*list

  • If you want it to encapsulate a list which is independent of anyone else's list anywhere in the universe, then it's probably reasonable for it to hold a list , but any time the structure is copied it will be necessary to free all the items that were held by the destination structure's employees list, copy the structure, and then for each node in the original list, allocate a new node, copy the data there, link the new node into a new list, and after that is done, make the new employees' *head and *tail point to the new list. 如果你希望它封装一个独立于宇宙中任何其他人列表的列表,那么它可能是合理的,它可以保存一个list ,但是每次复制结构时都需要释放所有持有的项目。通过目标结构的employees列表,复制结构,然后为原始列表中的每个节点分配新节点,在那里复制数据,将新节点链接到新列表,然后完成,创建新员工' *head*tail指向新列表。 Alternatively, one could make employees hold a *list ; 或者,可以让employees持有*list ; that would require one more allocation and release each time a struct is changed, but would allow the list copy and cleanup code itself to be better tied to the *list . 每次更改结构时需要再分配和释放,但是允许列表复制和清理代码本身更好地绑定到*list

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

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