←back to thread

48 points ingve | 1 comments | | HN request time: 0.309s | source
Show context
bobmcnamara ◴[] No.44389188[source]
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

replies(3): >>44389317 #>>44389346 #>>44389977 #
o11c ◴[] No.44389317[source]
Noncompliant, since `malloc(0)` is specified to return a unique pointer if it's not `NULL`.

On most platforms an implementation could just return adjacent addresses from the top half of the address space. On 32-bit platforms it doesn't take long to run out of such address space however, and you don't want to waste the space for a bitmap allocator. I suppose you could just use a counter for each 64K region or something, so you can reuse it if the right number of elements has been freed ...

replies(3): >>44389517 #>>44389553 #>>44395128 #
bobmcnamara ◴[] No.44389553[source]
> Noncompliant, since `malloc(0)` is specified to return a unique pointer if it's not `NULL`.

I know I've seen that somewhere, but may I ask what standard you're referring to?

If I recall correctly, this was an archaic stackless microcontroller. The heap support was mostly a marketing claim.

replies(3): >>44389646 #>>44389679 #>>44390133 #
jmgao ◴[] No.44389646[source]
C89: https://port70.net/%7Ensz/c/c89/c89-draft.html

If the size of the space requested is zero, the behavior is implementation-defined; the value returned shall be either a null pointer or a unique pointer.

replies(1): >>44389900 #
f1shy ◴[] No.44389900[source]
Isn’t -1 basically 0xffff which is a constant pointer? What am I missinterpreting?
replies(1): >>44389943 #
comex ◴[] No.44389943[source]
If you call malloc(0) multiple times (without freeing in between) and get -1 each time, then the pointer is not unique.
replies(3): >>44390238 #>>44392262 #>>44396934 #
1. ◴[] No.44390238[source]