Skip to main content
10 events
when toggle format what by license comment
Dec 5, 2021 at 10:57 comment added amon @TLW Using the preprocessor like that to simulate privacy is a really clever approach!
Dec 4, 2021 at 20:34 comment added TLW (I know in this particular case you can't stack-allocate the type anyway due to the ZLA.)
Dec 4, 2021 at 20:33 comment added TLW Also, this approach often adds a layer of indirection (you can't stack-allocate the type any more). A better approach in many cases is to (ab)use the preprocessor to make the fields within the struct inaccessible (struct string_t {string_data_t _string_internals;}; #define _string_internals PLEASE_DO_NOT_USE_STRING_INTERNALS_DIRECTLY (with an undef in the actual string implementation).) You can get around this (as always in C), but you're rather unlikely to accidentally do so.
Dec 4, 2021 at 20:29 comment added TLW "It is not possible to create a string except through the str_new() function," -> string *s = malloc(1); // yay C implicit pointer conversions.
Dec 2, 2021 at 21:41 comment added Deduplicator As an aside, if you pointed to the string-data (not the length before it) and added a terminator, it becomes quite similar to a BSTR.
Dec 2, 2021 at 9:44 comment added amon @Deduplicator I added a check for numeric overflow. Whether an extra null byte is needed depends on how that string is used. For example, I my example code doesn't zero out the string contents so it's not usable as a C-string anyway, just as a buffer.
Dec 2, 2021 at 9:40 history edited amon CC BY-SA 4.0
protect against numeric overflow
Dec 2, 2021 at 0:43 comment added Deduplicator string* s = malloc(sizeof(string) + len; That flies in the face of being robust. Also, it is off by one, as normally you would want a nul-terminator even if it is truly a counted string, as long as the cost is not too big.
Dec 1, 2021 at 21:40 vote accept Harf
Dec 1, 2021 at 21:40
Dec 1, 2021 at 21:13 history answered amon CC BY-SA 4.0