What if #C had a function like alloclen which returned the length of a memory allocation? That could simplify some parameter-passing!
#CLanguage
@golemwire I'm curious, in what context would you not have access to the requested memory size?
No existing context I know of, right now.

It makes sense to me that the data structuring used by a memory allocator would hold allocation length information, so it would be nice to be able to read it.

The current solution that's idiomatic in C works, but when you need to refer to the memory allocation you often need two separate variables, one for the pointer to the data, and the other for the length. This impacts parameter passing, references from other structs, etc.

It would be neat if the memory allocation length were exposed, and we could just pass around one value, the pointer. I would imagine it would reduce cognitive load on the programmer (and by extension, hopefully reduce bugs).

CC: @[email protected]
@golemwire @pixx I understand what you mean, but I feel like that only push the issue onto the language, which would have to hold a table with pointers and their allocated lengths rather than having the developer rolling their own solution adapted to their needs
Although I see where you come from, the length still need to be stored somewhere
Yeah, I don't think C really needs it; it's more of a reflection.

CC: @[email protected]

@golemwire @greenmoonmoon

By the way, if you're programming on plan9 you've been able to do this for decades:

extern ulong poolmsize(Pool*, void*);

you can just run `poolmsize(mainmem, alloc)` to get the size of an allocation that was malloced (since malloc gores through the mainmem pool)