←back to thread

49 points ingve | 9 comments | | HN request time: 0.205s | source | bottom
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 #
1. 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 #
2. f1shy ◴[] No.44389900[source]
Isn’t -1 basically 0xffff which is a constant pointer? What am I missinterpreting?
replies(1): >>44389943 #
3. 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 #
4. ◴[] No.44390238{3}[source]
5. bobmcnamara ◴[] No.44392262{3}[source]
But do we need a unique pointer or merely a pointer that is disjoint from all objects?
replies(1): >>44394909 #
6. david-gpu ◴[] No.44394909{4}[source]
As per the specification, it has to be a unique pointer.

Being tasked to implement a specification typically means having to pass extensive conformance tests and having to answer for instances of noncompliance. You soon learn to follow the spec to the letter, to the best of your abilities, unless you can make a strong case to your management for each specific deviation.

replies(2): >>44396834 #>>44396893 #
7. magicalhippo ◴[] No.44396834{5}[source]
But the letter is non-specific. It doesn't clarify if unique refers to unique when compared to non-zero allocations, or unique when called multiple times.

The C99 standard[1] seems to have worded it more precisely:

If the size of the space requested is zero, the behavior is implementation- defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

[1]: https://rgambord.github.io/c99-doc/sections/7/20/3/index.htm...

8. minetest2048 ◴[] No.44396893{5}[source]
This is embedded C where standard abuse is a thing: https://thephd.dev/conformance-should-mean-something-fputc-a...
9. mystified5016 ◴[] No.44396934{3}[source]
Null is not a unique pointer, it's a contant like -1

It returns multiple types of null pointer