News for hackers. Stuff that matters.
Re:Abstraction: Not what you think it is by ncmncm on Wednesday March 30, 2022 @11:19AM (#30856372)
If you will need to release it, then you also need to know it is a pointer that would afterward refer to freed storage.

Programmers understand that referents of pointers have lifetime that must be managed. Hiding that quality of your special-snowflake type does nobody any good.

Re:Abstraction: Not what you think it is by fluoridation on Wednesday March 30, 2022 @11:27AM (#30856474)
>If you will need to release it, then you also need to know it is a pointer that would afterward refer to freed storage.

You really don't. If the documentation says that after calling release_fluor(), the fluor_t that was passed to it can't be passed to get_fluor_mass(), it doesn't matter whether fluor_t is a pointer or not.

I'm sorry, but you're just plainly wrong.

Re:Abstraction: Not what you think it is by ncmncm on Wednesday March 30, 2022 @12:32PM (#30857488)
If you are relying on everybody who reads code that uses your type to have thoroughly scoured all the documentation, you are Doing It Wrong.

Programmers understand pointers and pointer semantics: when they see a pointer, they know what is required of them. An opaque type that has pointer semantics you can't tell without reading up on documentation is, exactly, an abstraction that costs more than it delivers.

Re:Abstraction: Not what you think it is by MaxBarraclough on Wednesday March 30, 2022 @05:42PM (#30861162)
> If you are relying on everybody who reads code that uses your type to have thoroughly scoured all the documentation, you are Doing It Wrong.

As fluoridation indicated, this is the norm in C programming. By means of example, in the OpenCL C library, bitfields all use the same type. You have to consult a function's documentation to know what you're allowed to pass. [0]

I do agree that the 'Ada-style' approach to types is far superior to the 'C-style', but Doing It Wrong seems a bit strong, unless you want to condemn all C code.

With the right library you can use the 'Ada-style' in C++. Specifically, using a 'strong typedef' library which prevents implicit conversions, such as [1], but not Boost's strong_typedef which permits implicit conversion back to the underlying type. Unfortunately this kind of thing isn't possible in C.

> Programmers understand pointers and pointer semantics: when they see a pointer, they know what is required of them

Not really. Should you call free on the pointer once you're done with it? The type alone doesn't tell you. You need to consult the documentation to know how you are meant to use the value, whether or not a pointer type is used.

[0] https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenC...

[1] https://github.com/foonathan/type_safe/

Re:Abstraction: Not what you think it is by ncmncm on Thursday March 31, 2022 @07:04AM (#30865850)
People know they need to find out whether and when they can or must free a pointer. If it looks enough like a file descriptor, they know they might need to call close() on it. For other types, there are no standard conventions.
Re:Abstraction: Not what you think it is by fluoridation on Wednesday March 30, 2022 @12:47PM (#30857691)
Okay. Have fun using C APIs without reading documentation and wondering why your programs keep crashing, I guess. What else can I say?
Re:Abstraction: Not what you think it is by ncmncm on Wednesday March 30, 2022 @01:09PM (#30857980)
I know why programs other people write keep crashing. They are coding them in C (or bad C++; but I repeat myself), and are relying on abstractions that conceal exactly that which they should instead expose.
Re:Abstraction: Not what you think it is by wvenable on Wednesday March 30, 2022 @01:17PM (#30858088)
Maybe the data type isn't a pointer, maybe it's an integer handle to a kernel object. And you may still need to free/release it even though it's not a pointer at all.

There are plenty of APIs that have similar semantics (connect/disconnect) that must be followed. And even if something is a pointer doesn't mean you can just free() it -- you might need to use whatever method the API provides to get proper operation.

This is the main abstraction mechanism in C programs -- the abstract data type. A piece of data you pass around but never know the innards of.

Re:Abstraction: Not what you think it is by ncmncm on Wednesday March 30, 2022 @01:44PM (#30858425)
File descriptors are an example of such a type. In good code they are carefully kept equal to -1 when not open, and are carefully identified, and not leaked.

When something has reference semantics but isn't an actual pointer, that is an extra fact that the reader of code needs to know in order to reason correctly about it. That is, thus, a burden above what would be needed for an actual pointer. It is a burden that is harmful to impose entirely unnecessarily.

Re:Abstraction: Not what you think it is by Koshkin on Wednesday March 30, 2022 @02:10PM (#30858731)
Pointers aren't special, and memory is not the only resource that requires managing.
Re:Abstraction: Not what you think it is by ncmncm on Thursday March 31, 2022 @06:59AM (#30865817)
Pointers are special. They have implicit conversions, and a wide variety of pre-defined operations, typically only a few of which are correct for any particular use.

Managing any resource is trivial with destructors. If you need to manage a resource, a pointer is a poor way to do it.

In C, you have a poverty of choices, but people are anyway used to being careful around pointers.