2 ms·
> C programmers don’t appear to have a name for it. So it is likely that you have never encountered it. The common name for this is "out parameters"
by kevincox 6y ago
> C programmers don’t appear to have a name for it. So it is likely that you have never encountered it.
The common name for this is "out parameters"
- masklinn 6y agoOut parameter would generally be possibly uninitialised memory in which the function will put the output data, rather than allocated memory which the function will fill. Out parameter is literally just a workaround for the lack of MRV, the callee still controls the allocation. For instance `pthread_create` uses an out parameter: pthread_t threadId; int err = pthread_create(&threadId, NULL, &threadFunc, NULL); the first parameter is uninitialised memory, it's only written to by the function. If C had tuples / MRV you'd say (int err, pthread_t threadId) = pthread_create(NULL, &threadFunc, NULL); instead. But might still use a "destination passing" function, because the point of destination-passing is to make the caller control the allocation, and allow amortisation. "Destination passing" is pretty common in low-ish level Rust APIs for instance, while "out" parameters is largely forbidden (you'd need a function taking a reference to a MaybeUninit or such nonsense).
- kevincox 6y agoI've commonly seen it used for both. Really anything that the function will write to.
- masklinn 6y ago> I've commonly seen it used for both That's a good reason to rename one of them because they're completely different semantics and behaviour, and confusing one for the other is a direct route to segfault city.
- comex 6y agoFrom another perspective, they're both performing the same operation on different types. With what you'd call "destination passing", like char *buf = malloc(128); snprintf(buf, 128, "hello"); the function is accepting a pointer to an uninitialized array of char, and initializing the array of char. With what you'd call an "out parameter", like char *buf; asprintf(&buf, "hello"); the function is accepting a pointer to an uninitialized pointer to array of char, and initializing the pointer. The caller still controls the allocation of storage for the pointer itself.
- arka2147483647 6y agoAt this point of the lifetime C-language, hoping for the commonly accepted terminology to change, is probably futile. Anyway, for out parameters, the common convention is: Caller provides one object for output: void callee(Obj *obj); Caller provides an array for output: void callee(Obj *obj, size_t obj_size); Caller provides an pointer, to which callee allocates a new object: void callee(Obj **obj)