News for hackers. Stuff that matters.
Re:Fat pointers in C using libcello by eqvinox on Wednesday April 29, 2020 @03:18AM (#23016335)
In my personal experience, "a bit more than a pointer" works best as a pair of (start, end) pointers (where "end" points to just beyond the last element.) The most obvious reasons for this are:

- slices become a total non-issue since a pair of (start, end) already is a slice and you can just move start and end.

- comparing against an end pointer is generally easier than adding up a length value first, particularly if you're slicing at the same time.

- the end pointer value is independent of the array element type, so if you e.g. cast to uint8_t * (which arguably you shouldn't in most cases) it stays exactly the same. If you store a count you need to adjust a multiplier. If you store a byte length, you need to do a lot of divides or casts to deal with pointer arithmetics.

Also, this is a huge red flag to me:

https://github.com/orangeduck/Cello/blob/master/include/Cell...

  #define is ==
  #define isnt !=
  #define not !
  #define and &&
  #define or ||
  #define in ,

P.S.: This also is a "try to invent a new programming language without inventing a new programming language" thing. Have your cake and eat it... either it's C or it isn't, and this library is leaving the space of "normal" C.
Re:Fat pointers in C using libcello by shawa_a_a on Wednesday April 29, 2020 @05:26AM (#23017164)
I don’t think you’re too far off with ‘leaving the space of normal C’, but I think it may help to see the context from which the author was coming when writing Cello [1] and evaluating it in that context.

It’s been a while since I watched the talk but I believe his intention was to do just that, to push the bounds of what could be done in a header file purely for the fun of it. The second half of the talk specifically addresses the “why are you doing this?” in quite a charming way.

1: https://youtu.be/bVxfwsgO00o

Re:Fat pointers in C using libcello by dllu on Wednesday April 29, 2020 @04:10AM (#23016682)
huh. Interestingly, for "and", "or" and "not", iso646.h already defines C alternative tokens: https://en.wikipedia.org/wiki/C_alternative_tokens
Re:Fat pointers in C using libcello by eqvinox on Wednesday April 29, 2020 @04:11AM (#23016692)
I know :) ... but have you seen anyone actually use those? I'd be curious to get a pointer or two.
Re:Fat pointers in C using libcello by saagarjha on Wednesday April 29, 2020 @04:24AM (#23016773)
Twice: once for a CTF challenge that stripped out certain symbols, and one other time when I set up an emulator with a sketchy key map and didn’t want to bother figuring out what those symbols were mapped to.
Re:Fat pointers in C using libcello by ritter2a on Wednesday April 29, 2020 @06:43AM (#23017645)
I've only seen wrong use: CS students working on a project in C++, using "and" and "or" as identifiers (which their local MSVC compiler apparently was cool with) and then being surprised that the course's testing environment (using gcc) found some errors.
Re:Fat pointers in C using libcello by dllu on Wednesday April 29, 2020 @04:13AM (#23016709)
I have seen people coming to C++ from Python use "and", "or", and "not" in their if-statements :)
Re:Fat pointers in C using libcello by eqvinox on Wednesday April 29, 2020 @04:17AM (#23016733)
Hah - I kid you not, I have the reverse problem! I work a lot with both C and Python (embedded interpreter & CPython extensions) and whenever I switch from a stretch of C coding to doing some Python, I catch myself typing "&&" and "||" instead of "and" and "or" :D

(C is my "native" language, so it doesn't really happen the other way around, but that's just me)

But honestly, I'm pretty sure we can call iso646.h "fringe" ;)

Re:Fat pointers in C using libcello by int_19h on Wednesday April 29, 2020 @06:47PM (#23025470)
Curiously, & and | are occasionally useful in Python. They do work properly with booleans, so the difference compared to "and" and "or" is that you don't get short circuiting, which in some very rare cases you might not want. ^ is less useful because it's almost the same as != (except stricter about types) - but it documents the intent more clearly.
Re:Fat pointers in C using libcello by aloisdg on Wednesday April 29, 2020 @05:30AM (#23017186)
In programming languages, does we have something akin to a mother-tongue?
Re:Fat pointers in C using libcello by macintux on Wednesday April 29, 2020 @11:30AM (#23020399)
There’s a fair bit of debate about the impact of learning specific programming styles as your first language. Quite a few people would argue that learning functional programming first is a benefit to understanding the concept of state and how to write clean code, even if you end up using something less pure.

Certainly, speaking as someone brought up on imperative and later OO code, it took a fair bit of unlearning to understand functional programming, and I still have no clue about type theory.

Re:Fat pointers in C using libcello by jcelerier on Wednesday April 29, 2020 @06:52AM (#23017699)
what's the problem ? it's legal C++ (https://en.cppreference.com/w/cpp/language/operator_alternat...) and imho more readable than && / ||
Re:Fat pointers in C using libcello by eqvinox on Wednesday April 29, 2020 @08:50AM (#23018471)
We're talking about C here, not C++. The keywords don't exist in C unless you #include "iso646.h"
Re:Fat pointers in C using libcello by pjmlp on Wednesday April 29, 2020 @10:53AM (#23019974)
So what? Unless you include std...h you also need to use _Bool, _Complex and so forth, hardly much different.
Re:Fat pointers in C using libcello by int_19h on Wednesday April 29, 2020 @06:48PM (#23025478)
But the repo doesn't include them - it defines them itself.

Technically, I believe this is actually UB, just like any other #define for a keyword or an identifier from the standard library.

Re:Fat pointers in C using libcello by pjmlp on Thursday April 30, 2020 @02:49AM (#23028111)
I don't follow.

When I do #include <stdbool.h> it also brings the #define bool _Bool, a keyword from the standard library.

How is it UB?

Re:Fat pointers in C using libcello by int_19h on Friday May 01, 2020 @09:32PM (#23048295)
It's not UB to #include standard headers that do such things. It is UB to #undef or redefine them yourself, though. And it looks like <stdbool.h> gets an explicit exemption, at least in C11:

"Notwithstanding the provisions of 7.1.3, a program may undefine and perhaps then redefine the macros bool, true, and false"

I guess it's because defining these was so common even before C99. But there's no similar verbiage for <iso646.h>, so...

Re:Fat pointers in C using libcello by jcelerier on Wednesday April 29, 2020 @08:56AM (#23018525)
let me quote you the comment I was replying to :

> I have seen people coming to C++ from Python use "and", "or", and "not" in their if-statements :)

Re:Fat pointers in C using libcello by eqvinox on Wednesday April 29, 2020 @09:50AM (#23019140)
Yet curiously absent from that quoted comment is anything about it being a problem, which you nonetheless ask about. I wonder if more context or a more well-intentioned reading is the solution? ;)
Re:Fat pointers in C using libcello by jcelerier on Wednesday April 29, 2020 @10:38AM (#23019769)
I read and reread it and I honestly don't understand how it is possible to come up with an interpretation where that is not seen as an issue by the author
Re:Fat pointers in C using libcello by aaron-lebo on Wednesday April 29, 2020 @03:55AM (#23016583)
I'll admit I've never written more than small programs in C, but the criticism that this "isn't C" isn't a fair criticism to me. He's not doing anything more than any other library can do, were he to write a compiler that mapped directly to really boring C, would it be more or less C than this? I don't feel like those questions are useful. We need experiments like this, and for me personally, cello was a revelation when it was posted here years ago. There are no rules that say you can't do it.

I know he's doing a little hackiness by placing the size before the start of the object, but they also take that approach here (https://www.piumarta.com/software/cola/objmodel2.pdf) so maybe it's not that uncommon. How would you do the dual pointer setup? Is there any overhead from that, or is it small enough to not worry about?

Re:Fat pointers in C using libcello by eqvinox on Wednesday April 29, 2020 @04:04AM (#23016646)
The "isn't C" argument is more about the library as a whole, not specifically about the fat pointer suggestions. (Please look at the github repo, IMHO it's obvious.)

Placing a length "before" a pointer is perfectly fine on a _technical_ level. It's also how glibc's malloc works, it has its own data before any allocation it returns. However, hackiness is not the question here - it's whether it's the "best" approach. I simply believe, based on my own experiences, that twin pointers cover/win out in a much larger subset of applicable scenarios.

As for how to implement it - declare a struct with 2 pointers in it. Or just pass 2 pointers around.

Re:Fat pointers in C using libcello by artemonster on Wednesday April 29, 2020 @05:36AM (#23017213)
Do you, by chance, know where else object model of Piumarta is used? I think the Per6 VM "Potion" used it too as a base, but I am unsure
Re:Fat pointers in C using libcello by aaron-lebo on Wednesday April 29, 2020 @05:45AM (#23017284)
Unfortunately, no, your info about Perl 6 is news to me. Only ran into it a few months ago, tried it in Python and Nim, but haven't seen it in the wild. It's very cool though.
Re:Fat pointers in C using libcello by artemonster on Wednesday April 29, 2020 @05:57AM (#23017367)
Re:Fat pointers in C using libcello by gumby on Wednesday April 29, 2020 @03:19PM (#23023018)
> In my personal experience, "a bit more than a pointer" works best as a pair of (start, end) pointers (where "end" points to just beyond the last element.)

There’s a PARC paper from the Cedar team benchmarking base-and bounds vs marker-terminated (e.g. null terminated) strings and base-and-bounds won hands down on a variety of use cases. Won on speed, not just the safety grounds. In those days some people actually worried about the space taken up by the extra bounds variable.

Re:Fat pointers in C using libcello by pjmlp on Wednesday April 29, 2020 @04:01PM (#23023500)
While people did worry about the space taken up by the bounds checking information, systems 10 years older than the PDP-11 had enough hardware resources to support high level systems programming languages with bounds checking data structures, let alone the beefy PDP-11 (by comparison with those older models).
Re:Fat pointers in C using libcello by gumby on Wednesday April 29, 2020 @04:26PM (#23023817)
As I know well as a KA-10 MACLISP programmer!

But most people these days have a hard time realizing what it was like to program a machine with only a few K of memory. PL/1 (the Multics implementation language) had bounded strings. C did not as it was believed the PDP-7 couldn't afford it. Of course by today's standards the GE 645 is an absurdly tiny machine.

Re:Fat pointers in C using libcello by pjmlp on Thursday April 30, 2020 @02:46AM (#23028097)
Yeah, that was my point, this knowledge is getting lost, only to be re-discovered by those that love to dig into computer history.

Just to think that what is now on a ESP32 die took over a complete desk at the high school computer club and was still weaker than what ESP32 is capable of, yet that mentality also persists for what 80's hardware was already capable of.

Re:Fat pointers in C using libcello by kortex on Wednesday April 29, 2020 @09:57AM (#23019241)
This is how Go does slices, though they are 3 fields:

    type slice struct {
        zerothElement *type
        len int
        cap int
    }
Though I suspect under the hood the len and cap are ordered first.

If it's good enough for C greybeard Ken Thompson and unix hacker Rob Pike, its good enough for me.

In fact I've looked for a port of go-style slices to C and haven't found one. Maybe people think sds is good enough?

Re:Fat pointers in C using libcello by LukeShu on Wednesday April 29, 2020 @10:19AM (#23019541)
> Though I suspect under the hood the len and cap are ordered first.

A slice is stored in memory as a `reflect.SliceHeader` https://golang.org/pkg/reflect/#SliceHeader ; the pointer does come first.

Re:Fat pointers in C using libcello by jgbaldwinbrown on Wednesday April 29, 2020 @01:18PM (#23021678)
I actually wrote a (sort of) Go-style slice library. It's a little heavier than Go slices because it allows for dynamic array resizing and tracking the parentage of slices:

https://github.com/jgbaldwinbrown/slice

It's just a proof-of-concept, though, and would need a lot of work to be used in anything serious.

Re:Fat pointers in C using libcello by int_19h on Wednesday April 29, 2020 @06:50PM (#23025489)
Go has a rather idiosyncratic take on arrays and how they're used, which is reflected in its slices. I can't think of any other language or framework that did it this way.
Re:Fat pointers in C using libcello by saati on Thursday April 30, 2020 @02:38AM (#23028054)
Rust Vec has the same representation.
Re:Fat pointers in C using libcello by hannibalhorn on Wednesday April 29, 2020 @06:42AM (#23017634)
Start and end pointers is the approach used by the C++ STL, too.
Re:Fat pointers in C using libcello by vardump on Wednesday April 29, 2020 @04:06AM (#23016661)
> Also, this is a huge red flag to me:

Puke! Who would ever want to create that kind of macro abomination.

Re:Fat pointers in C using libcello by dgellow on Wednesday April 29, 2020 @04:25AM (#23016782)
Bjarne Stroustrup! https://en.wikipedia.org/wiki/C_alternative_tokens

And I'm not even joking :)

Re:Fat pointers in C using libcello by arcticbull on Wednesday April 29, 2020 @05:46AM (#23017288)
Man Bjarne Stroustrup is like the guy in college who flatly refused to conform in any way whatsoever no matter how good or bad it would have been for 'em. It's more of an oppositional personality disorder ;) That kid grew up and invented, because of course he did, C++.
Re:Fat pointers in C using libcello by int_19h on Wednesday April 29, 2020 @06:55PM (#23025539)
It might have something to do with various symbols that C uses not being convenient to type in many non-US keyboard layouts. In the standard Danish one, ~|^ all require the use of AltGr.
Re:Fat pointers in C using libcello by arcticbull on Wednesday April 29, 2020 @08:43PM (#23026311)
C++ isn't exactly short on symbols haha
Re:Fat pointers in C using libcello by vardump on Wednesday April 29, 2020 @04:47AM (#23016917)
Yeah, damn, just found out myself. I thought I knew C inside out, but I guess there are still bits and pieces you just don't encounter very often.

That said, my opinion about those macros is unchanged.

Re:Fat pointers in C using libcello by garaetjjte on Wednesday April 29, 2020 @07:01AM (#23017761)
Re:Fat pointers in C using libcello by vardump on Wednesday April 29, 2020 @03:35PM (#23023223)
Pretty curious: someone seriously disagrees this header should not exist? I'd really love to hear the arguments!