4 ms·
Why would alloca be more manual than allocating a sufficiently large statically sized array, or allocating variable stack memory with VLAs?
by mort96 4y ago
Why 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.
- mort96 4y agoCan you respond to the second part of my comment? 'char buf[size]' seems just as explicit as 'char *buf = alloca(size)' yet you're comfortable claiming that one constitutes automatic memory management and the other manual memory management? How is it not just two slightly different syntaxes for the same thing? I'm really struggling to understand how there's such a big fundamental difference.
- coldtea 4y ago>How is it not just two slightly different syntaxes for the same thing? Well, anything that ends up allocating some memory for you and giving you back handler would be also "slightly different syntax for the same thing". I'd say that the first case abstracts away the allocation though, while the second makes it explicit. It's just too small an example to be that big of a difference. Another thing that comes to mind, is that you can just write: char buf[4] = "foo"; to initialize the string, whereas with alloca you'd need something like: char* buf = alloca(4); strcpy(buf, "foo"); Isn't the latter quite more manual?
- mort96 4y agoSo you think two different syntaxes for allocating N bytes of automatically managed (i.e stack) memory are in fundamentally different classes of memory management because one is slightly more explicit than the other (because a pointer is returned or something I guess?). I don't agree, but at this point, it's semantics. In the literature though, you may find that "manual" memory management usually refers to memory management where the programmer has to call a 'free'-style function manually, while automatic memory management usually refers to anything where the programmer doesn't have to call 'free' (be it thanks to lifetimes or GC or automatic refcounting or whatever).