by bobmcnamara
3 subcomments
- Ages ago I worked with a system where malloc(0) incremented a counter and returned -1.
free(-1) decremented the counter.
This way you could check for leaks :p
- I get the complexity of the standards issue here, but if you cared about this, wouldn't you just wrap malloc with something trivial that provided the semantic you wanted to depend on (NULL or some sentinel pointer).
- Not the best choice to begin the title with "some bits" in this context. My mind was trying to understand this sentence in a completely different way...
by randomNumber7
2 subcomments
- I never had the use case to allocate 0 bytes of memory.
If I would allocate 0 bytes of memory and get a pointer to it, I wouldn't care what the value of the pointer is since I am not allowed to dereference it anyways.
But then again, why would I allocate 0 bytes of memory?
by Lvl999Noob
2 subcomments
- Can someone tell me a usecase where you want multiple allocations of size 0, each one with a unique address, and each one unique from any other allocation (hence necessarily removing that pointer from being allocated to anything else) but can't use malloc(1) instead?
I think it would be much better if malloc(0) just returned 1 or -1 or something constant. If the programmer needs the allocation to have a unique address, they can call malloc(1) instead.
by AaronDinesh
5 subcomments
- Why should it be allowed to return a valid pointers anyways? Surely it should always return NULL?
- would be interesting to see if there's a difference in how the 0-page is handled in systems under this condition...
- I maintained a program which failed on, as I recall, AIX (mentioned in the essay) because malloc(0) returned NULL.
It's been 30 years so I've forgotten the details. My solution was to always allocate size+1 since memory use was far from critical.