Re:Once upon a time, memory allocators made sense
by akeruu on Monday January 18, 2016 @07:29AM
(#10923985)
To me, the insanity is trying to allocate zero byte
[ Parent
| on HN ]
Re:Once upon a time, memory allocators made sense
by zAy0LfpBZLC8mAC on Monday January 18, 2016 @08:13AM
(#10924161)
That'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.
Re:Once upon a time, memory allocators made sense
by tbirdz on Monday January 18, 2016 @09:02AM
(#10924392)
A 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.
Re:Once upon a time, memory allocators made sense
by carussell on Monday January 18, 2016 @09:16AM
(#10924472)
The example is clearly forgoing the use of standard C strings for a char array tagged with the length.