简体   繁体   中英

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.

Is there anything else that should be taken into account?

Edit: list is a single-linked 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.

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

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.

  • 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 .

  • 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 .

  • 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. Alternatively, one could make employees hold a *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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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