6 ms·
Not for programs where the amount of items you must store is only known at run time.
by bruce343434 4y ago
Not for programs where the amount of items you must store is only known at run time.
- rkta 4y agoC99 has VLAs and there is always man 3 alloca
- pjmlp 4y agoDeprecated in C11 as it was acknowledged how bad idea they were in first place.
- coldtea 4y agoC99 is a much newer standard though. 1911 C was very primitive, pre-ansi C, basically an assembler for Babbage machines.
- pjmlp 4y agoLooking forward to the Analytical Engine compiler backend punch cards.
- rkta 4y agoVLAs have been deprecated, alloca was never part of a C standard AFAIK. I was not suggesting that any of both is a good idea - but they are tools 'for programs where the amount of items you must store is only known at run time.' (Assuming that not having to free is considered "automatic memory management").
- mort96 4y agoDeprecated? I thought they were only made optional?
- pjmlp 4y agoWhich allows any compiler that never implemented C99 to be certified as compliant on later standards (C11, C17, C23), thus yielding the same outcome in practice.
- mort96 4y agoThe status of VLAs is officially the exact same as the status of 'uint32_t' and the like, so I think it's fair to say there's a significant difference between something being optional (a la uint32_t) and something being deprecated (a la gets). But I agree, in practice, the change to VLAs feels more deprecation-like, because they got demoted to optional because everyone more or less recognised that they're a bad idea, I don't imagine GCC will remove it any time soon but I wouldn't be surprised if it got options to disable them and if those options eventually became enabled by default.
- rkta 4y agoCorrect, they are optional, not deprecated. That was sloppy on my side.
- coldtea 4y agoIf you use alloca that's not "automatic memory management" is it?
- rkta 4y agoInteresting. If I think 'automatic memory management' in C I think about not having to free. Of course you are right that it's not the same as using e.g. a Python List.
- mort96 4y agoWhy would alloca be more manual than allocating a sufficiently large statically sized array, or allocating variable stack memory with VLAs?
- coldtea 4y agoWell, for starters, because you expicitly call to allocate. A static array is implicitly allocated by saying you want an array. Furthermore, neither does any memory management, the memory is retained till the end of the program. "Simple memory acquisition" perhaps, "memory management" not, except if we accept it as a degenerate case, where things aren't even freed except when the program ends.
- mort96 4y agoYou may be thinking of malloc. Memory allocated by alloca is allocated on the stack, so it lives until the end of the stack frame. And the difference between 'char buf[size]' and 'char *buf = alloca(size)' isn't significant enough to put the two in separate paradigms IMO, just slightly different ways of writing the same thing.
- coldtea 4y ago>You may be thinking of malloc. Memory allocated by alloca is allocated on the stack, so it lives until the end of the stack frame. I see, but you still have to ask nicely for it explicitly.
- bruce343434 4y agoThere's still significant drawbacks, mostly that the data only lives during the lifetime of the scope.
- pkkm 4y agoThese are usually discouraged. If there's small upper bound on the size you'll need, type array[MAX_SIZE] is more efficient and harder to mess up [0]. If there isn't, you should use the heap. [0] For more details, see e.g. https://nullprogram.com/blog/2019/10/27/ https://nullprogram.com/blog/2019/10/27/
- rasz 4y agoVLAs dont seem to be all that hot. https://news.ycombinator.com/item?id=18323938 https://news.ycombinator.com/item?id=18323938 The Linux Kernel Is Now VLA (Variable-Length Array) Free (phoronix.com)
- gpderetta 4y agofork(), malloc(), exit() when done.
- coldtea 4y agoHow is that "automatic memory management"?
- gpderetta 4y agoIt is a brute force arena allocator. No free needed. But mostly a joke.
- flohofwoe 4y agoIt's a completely valid memory management strategy for "one-shot" command line tools though.
- rwmj 4y agoIt is. Unfortunately when I tried to use it in a particular product, I got so much negative feedback from our Coverity and other static analysis tools that I have given in and added calls to free everywhere.
- mort96 4y agoIt's more than that. It's a "valid" strategy even for long lived servers. Fork, do the thing which requires heap memory in the child, then exit the child (but the parent keeps running).
- wheelerof4te 4y agoWith smart missiles, you don't even need that exit().
- alexeiz 4y agoWho needs exit() when you have kill().
- ihatepython 4y ago> Not for programs where the amount of items you must store is only known at run time. Usually it's not necessary to load everything into memory. It's usually more efficient to process data similar to map-reduce, and just use stdio and stdout