News for hackers. Stuff that matters.
Re:Show HN by gorgoiler on Saturday December 03, 2022 @04:20PM (#33847486)
Whoa. Jamming metadata in the address space before the string pointer is such a clever idea. I don’t know enough about C to know how many awkward bugs this might cause, but I know enough about programming to spot exceptional lateral thinking when I see it. Very neat.

I guess the SDS authors might ship a linter to spot all the times you mistakenly use free() instead of sdsfree()? That could make the cleverness more tolerable?

Re:Show HN by arcticbull on Saturday December 03, 2022 @04:31PM (#33847590)
This is a common approach for things like malloc to use, since you are passing an opaque pointer to arbitrary data into free() which you then expect to quickly do something useful with. It can just walk back the pointer a little to find the header and act on it.

It's pretty weird to see it anywhere other than malloc though especially masquerading as a basic type. It's incompatible with other common patterns like returning via (char *) and you can't identify which deallocator you're supposed to give the result to from the type alone.

Re:Show HN by Gibbon1 on Saturday December 03, 2022 @06:34PM (#33848679)
Random musing of a old firmware guy. In the past I've had issues with wanting to make sure a function isn't being passed a pointer to an object on the stack. Least in embedded land it's trivial to write a function that can tell you if an address points to something on the stack, heap, or is a global.
Re:Show HN by deterministic on Sunday December 04, 2022 @06:19PM (#33859149)
This is actually a very well known C trick.