You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found out the **p can be removed, which leads to lesser codes, but I don't know whether it's better or worse since it introduces a warning, what do you think?
#23
Open
Camio1945 opened this issue
Oct 21, 2023
· 2 comments
This is actually an interesting question. If we look at the definition of list:
structlist {
list_item*head;
};
we could find that list is a struct with list_item * as its first (and only) member. This means list is nothing but a wrapper around list_item * and they could be used interchangeablely to some extent.
For instance, given p of type list_item ** and l of type list *, and they share the same value, one could conclude that *p is equivalent to l->head since both expressions return the pointed value (of type list_item *). This observation leads to the code in your question:
// Original version with best readability.list_item**find_indirect_v1(list*l, list_item*target) {
list_item**p=&l->head;
while (*p!=target) p=&(*p)->next;
returnp;
}
// `list` is just a wrapper around `list_item *`.// Use explicit cast to suppress warnings.list_item**find_indirect_v2(list*l, list_item*target) {
while (l->head!=target) l= (list*)&(l->head)->next;
return (list_item**)l;
}
// What if `list' is really `list_item *`?// Compare this with the original version.typedeflist_item*list_v3;
list_item**find_indirect_v3(list_v3*l, list_item*target) {
while (*l!=target) l=&(*l)->next;
returnl;
}
Back to your question, is this implementation (find_indirect_v2 in the above code) better since we eliminated the use of a local variable (p in the example) compared to the original version? No (at least with modern compilers). All of the three implementations above produces same (optimal) assembly code with Clang in O1 optimization.
So general guideline is to perfer readability over some small and questionable optimizations like above as the compiler could do a better job in most of the time. (As a side note, the code in find_indirect_v2 is legal as long as list and head share same address, which is the case in our example.)
with
**p
:without
**p
:It works, but it introduces a warning:
I'm not familiar with C, I don't know whether it's better or worse, what do you think?
The text was updated successfully, but these errors were encountered: