Most active commenters
  • bobmcnamara(3)

←back to thread

48 points ingve | 12 comments | | HN request time: 1.755s | 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 #
1. 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 #
2. 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 #
3. fredoralive ◴[] No.44389679[source]
Presumably the ANSI C standard or one of the later editions? They also cover the standard library as well as the language. (Presumably the bit about "Each such allocation shall yield a pointer to an object disjoint from any other object." if the random C99 draft I found via google is accurate to the final standard - I suppose you might question if this special use is technically an allocation of course).

Of course, microcontrollers and the like can have somewhat eccentric implementations of languages of thing and perhaps aren't strictly compliant, and frankly even standard compliant stuff like "int can be 16 bits" might surprise some code that doesn't expect it.

4. f1shy ◴[] No.44389900[source]
Isn’t -1 basically 0xffff which is a constant pointer? What am I missinterpreting?
replies(1): >>44389943 #
5. comex ◴[] No.44389943{3}[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 #
6. o11c ◴[] No.44390133[source]
(you duped your comment under the other subthread)

From C89, §7.10.3 "Memory management functions":

> 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.

The wording is different for C99 and POSIX, but I went back as far as possible (despite the poor source material; unlike later standards C89 is only accessible in scans and bad OCR, and also has catastrophic numbering differences). K&R C specifies nothing (it's often quite useless; people didn't actually write against K&R C but against the common subset of extensions of platforms they cared about), but its example implementation adds a block header without checking for 0 so it ends up doing the "unique non-NULL pointer" thing.

7. ◴[] No.44390238{4}[source]
8. bobmcnamara ◴[] No.44392262{4}[source]
But do we need a unique pointer or merely a pointer that is disjoint from all objects?
replies(1): >>44394909 #
9. david-gpu ◴[] No.44394909{5}[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 #
10. magicalhippo ◴[] No.44396834{6}[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...

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

It returns multiple types of null pointer