4 ms·
To me, the insanity is trying to allocate zero byte
by akeruu 11y ago
To me, the insanity is trying to allocate zero byte
- bjornsing 11y agoAgree, but that's not the allocator's sanity though.
- zAy0LfpBZLC8mAC 11y agoThat's because you think of it as a special case, which it isn't. If you want to store a string that's n bytes long, you need to allocate n bytes, so a natural thing to write is, for example dst.len=strlen(src);dst.buf=malloc(dst.len); --it's insanity if you need to add a special case for strings of a specific length.
- tbirdz 11y agoA standard C string will never be 0 bytes. There is the zero terminator at the end of the string, so all strings will be at least 1 byte large.
- carussell 11y agoThe example is clearly forgoing the use of standard C strings for a char array tagged with the length.
- acqq 11y agoYou don't have to call malloc directly in the second assignment. If you depend on special handling of null without the "if" on invocation you can have your own function for that.