top | item 39721845

(no title)

alchemio | 1 year ago

Just a correction: most std C functions don’t allocate. strdup does but it was only recently adopted into the standard, it was previously an extension.

Similarly zig’s stdlib shouldn’t allocate behind your back, except for thread spawn where it does: https://github.com/ziglang/zig/blob/5cd7fef17faa2a40c8da23f0...

Generally speaking, it’s as mentioned just a convention. A zig library might not allow its users to pass allocators for example.

In C++, stl containers can take an allocator as a template parameter. Recent C++ versions also provide several polymorphic allocators in the stdlib. You can also override the global allocator or a specific class’ allocator (override placement new).

discuss

order

AndyKelley|1 year ago

For spawning a thread you're literally asking for the stack of the thread to be memory mapped. I fail to see how this is "behind your back".

You also linked specifically to the POSIX threads implementation of thread spawning, which is by definition supposed to play nicely with the libc posix threads API, which expects you to use the libc allocator in combination with POSIX threads API, so that's what it does.

You might as well accuse the mmap() function in the zig standard library of allocating behind your back.

alchemio|1 year ago

It’s something Zig touts when compared to other languages(1). The idea is that in the end it’s a convention that an allocator needs to be passed to indicate that the function allocates, which not even the stdlib adheres religiously to. I’m fine with it since I do believe a library writer should know best what works with their library.

1. https://ziglang.org/learn/why_zig_rust_d_cpp/#no-hidden-allo...

MindSpunk|1 year ago

The behind your back part is probably referring to the Args payload bouncing through a heap allocation. It isn't explicit on the signature it's making an allocation. The function has no choice though unless you leave it up to the user to keep the payload allocation live until the thread terminates.

fsckboy|1 year ago

he was pointing out that it does, and he was not applying to it the label "behind your back, that's why he said "except for". the wording makes perfect sense.

jcelerier|1 year ago

> strdup does but it was only recently adopted into the standard, it was previously an extension.

Does it really matter when people were already writing code with strdup when the zig and rust creators were in middle school ? It was already there in BSD 4.3 (1986) apparently.

uecker|1 year ago

GNU obstacks also already exist for as long as I can remember.

Everything old is new again...

alchemio|1 year ago

It really doesn’t matter and that’s my point.