News for hackers. Stuff that matters.
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
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.
Re:Once upon a time, memory allocators made sense by acqq on Monday January 18, 2016 @11:08AM (#10925145)
You 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.
Re:Once upon a time, memory allocators made sense by bjornsing on Monday January 18, 2016 @07:54AM (#10924086)
Agree, but that's not the allocator's sanity though.