3 ms·
Hang on, let me think this through... If malloc(0) gets called as first malloc in the program the system break does not need to be moved, as there is always 0
by tmtvl 4y ago
Hang on, let me think this through...
If malloc(0) gets called as first malloc in the program the system break does not need to be moved, as there is always 0 bytes space available... but malloc does like to move sysbreak by a large amount at a time to reduce the need for repeated calls...
I'm guessing malloc(0) does not move sysbreak and simply returns a pointer to the bottom of the heap?
- monocasa 4y agoImplementation defined. I've heard of returning null (under the case that your free() implementation allows nulls to be passed in) or returning a pointer to a zero length object on the heap like you're suggesting. Really just about the only requirement is that the pointer can subsequently be given to free() since dereferencing the pointer is UB.
- int_19h 4y agofree(NULL) is required to be a no-op by the ISO C standard.
- monocasa 4y agoOh, good call. I had that backwards in my memory. The issue is when you give out non NULL pointers to zero sized objects, you have to make sure to give out unique pointer bit patterns at least versus nonzero sized objects so that the matching calls to free don't stomp on eachother.
- zabzonk 4y agomalloc is a user level library function - c/c++ implementers can do what they like with it assume the downvote was from someone that malloc is a system call
- loeg 4y agoI downvoted for the assumption that sbrk is involved in any way. It's an implementation detail of (some) historical malloc implementations, not some inherent aspect of all implementations. The entire thought experiment is faulty.
- tmtvl 4y agoRight, malloc uses mmap instead of sbrk, I'm an idiot. I really ought to read up on up-to-date implementation practices.
- p0nce 4y agoJust want to point out malloc(0) might gives you any pointer, and you still have to call free() on it.