Re:The Evolution of C Programming Practices: A Study of Unix...
by simias on Friday May 11, 2018 @08:20AM
(#17046706)
Yeah their take on goto is a bit odd given that it's probably the sanest way to do "cascading" error handling in C given that we don't have RAII or exceptions.
[ Parent
| on HN ]
Re:The Evolution of C Programming Practices: A Study of Unix...
by nierro on Friday May 11, 2018 @10:51AM
(#17047678)
GCC supports RAII through cleanup variable attribute (https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attribute...) though.
Re:The Evolution of C Programming Practices: A Study of Unix...
by enriquto on Friday May 11, 2018 @09:20AM
(#17047085)
For small and moderate memory requirements, C pretty much supports RAII via variable-length arrays.
You just replace this:
void f(int n)
{
float *x = malloc(n * sizeof*x);
// ...
free(x);
}
with this: void f(int n)
{
float x[n];
// ...
}
edit: and you can exit your function anywhere without leaking.
Re:The Evolution of C Programming Practices: A Study of Unix...
by pjmlp on Friday May 11, 2018 @11:19AM
(#17047908)
They were made optional, as it turned out a very bad idea regarding security.
I just need to call your example function with the right n to corrupt the stack.
Re:The Evolution of C Programming Practices: A Study of Unix...
by simias on Friday May 11, 2018 @09:34AM
(#17047179)
For memory allocs sure, but that's only a small subset of resource management. How about sockets, fds, locks, 3rd party library initialization, hardware setup (for device drivers) etc... Alloca doesn't cut it, you need general purpose destructors.
Re:The Evolution of C Programming Practices: A Study of Unix...
by Const-me on Friday May 11, 2018 @10:34AM
(#17047550)
Also when you code something at lower levels, sometimes it’s beneficial to treat some parts of CPU state and thread state as resources: process/thread priority, interrupt mask, FPU control, even instruction set (e.g. Thumb support on ARM needs to be manually switched on/off).
Re:The Evolution of C Programming Practices: A Study of Unix...
by enriquto on Friday May 11, 2018 @09:40AM
(#17047215)
sure, it's just a small, but non-negligeable, part of RAII that you can do in C
Re:The Evolution of C Programming Practices: A Study of Unix...
by zzzcpan on Friday May 11, 2018 @10:19AM
(#17047447)
It's negligible, you still need to manage lifetimes somehow beyond the scope of a single function. Like having a context abstraction and tying destruction of resources to it. Introducing something alternative for special cases only increases complexity as now instead of using a single universal and consistent API you have multiple that behave rather differently.
Re:The Evolution of C Programming Practices: A Study of Unix...
by enriquto on Friday May 11, 2018 @10:28AM
(#17047509)
in many cases (I would say most, at least for my programs in non-interactive scientific computing), all the objects can be created at the beginning of the program, and then no further creation happens. Sometimes it takes a bit of effort to refactor your program into that structure, but it is an effort well spent. Then you can use tools like openbsd's pledge, and reason more clearly about your algorithms.
Re:The Evolution of C Programming Practices: A Study of Unix...
by simias on Friday May 11, 2018 @10:53AM
(#17047692)
I concur, that tends to be my modus operandi as well but unless your application is completely monolithic you'll probably have 3rd party init code to deal with at some point. And again it won't help if you need to handle cleanup that's not memory-related.
In the case of an operating system (the subject of TFA) pre-allocating everything is obviously completely impractical and alloca won't help since you can't return the memory outside of the stack frame. I'd wager that there are very few uses of goto in kernel code that could successfully be replaced by alloca (the fact that kernel stacks tend to be very shallow wouldn't help either).
Re:The Evolution of C Programming Practices: A Study of Unix...
by Too on Sunday May 13, 2018 @03:46AM
(#17057792)
Pre allocation is generally the safe option in a embedded security critical environment where you must always handle the worst case scenario and you know all possible inputs. In a user interaction environment though it's usually better to over-sell so that the user can choose wheter he wants to create 1 million A's or 1 million B's, instead of having a pre-created pool of half a millon A and Bs each.
With pre allocation you usually also end up creating your own resource management within the pre allocated pool and then you are back to the resource management problem...
Re:The Evolution of C Programming Practices: A Study of Unix...
by richard_todd on Friday May 11, 2018 @09:35AM
(#17047184)
RAII is about a lot more than freeing memory. What if that’s an array of open file handles? Or mutex locks, etc?
Re:The Evolution of C Programming Practices: A Study of Unix...
by tom_ on Friday May 11, 2018 @10:46AM
(#17047642)
VLAs are optional in C11: https://port70.net/~nsz/c/c11/n1570.html#6.10.8.3