3 ms·
Do you convert them every time? No, the article addresses this: since the memory layout of the first two struct members is the same in both structs, you can us
by kelnos 2y ago
Do you convert them every time?
No, the article addresses this: since the memory layout of the first two struct members is the same in both structs, you can use a pointer to str_buf anywhere a function calls for a pointer to str, after casting it.
- teo_zero 2y ago> you can use a pointer to str_buf anywhere a function calls for a pointer to str Yes, you could, but I see no function mentioned in TFA that wants a pointer to str, only functions that want a str: print_str(), print_fmt(), com_write(). At the same time, the functions that return strings return a struct, never a pointer: str_new(), str_from_range(), str_from_buf(), fmt_buf_new(), and the pseudo-function STR(). To use the memory layout trick you should go through reference + cast + dereference: *((struct str *)&...) My question still holds: is the code littered by such conversion artifacts?