4 ms·
Because C has automatic variables. https://en.wikipedia.org/wiki/Automatic_variable https://en.wikipedia.org/wiki/Automatic_variable For the argument "that's a
by bjourne 8y ago
Because C has automatic variables. https://en.wikipedia.org/wiki/Automatic_variable https://en.wikipedia.org/wiki/Automatic_variable For the argument "that's a different kind of memory" see this thread from last week: https://news.ycombinator.com/item?id=18124431 https://news.ycombinator.com/item?id=18124431 It is entirely possible to write complex C programs using nothing but automatic variables.
- zbentley 8y agoFrom the wiki: > In C, using the storage class register is a hint to the compiler to cache the variable in a processor register. Other than not allowing the referencing operator (&) to be used on the variable or any of its subcomponents, the compiler is free to ignore the hint. Doesn't sound like a garbage collector to me, by any stretch of the imagination.
- berti 8y agoThe register hint is a bit of a semi-unrelated sidetrack, which you would only ever use in a really hot location when proven necessary by profiling + inspection of the generated code, and the compiler is more often than not smarter than you anyway so it's rarely necessary. It's not relevant to GPs point about regular locals being stored on the stack. See https://en.wikipedia.org/wiki/C_syntax#Storage_class_specifiers https://en.wikipedia.org/wiki/C_syntax#Storage_class_specifi... for a better overview.
- kaoD 8y agoThis is not garbage collection because these are stack (or statically) allocated. The "garbage" in "GC" are heap allocations. If we ignore the heap in C it doesn't make it a GC'd language because the concept of GC isn't even defined with no heap. > It is entirely possible to write complex C programs using nothing but automatic variables For a very narrow subset of "complex". As soon as dynamically-sized type is introduced you need the heap. It's also surprisingly inefficient: suddenly all your buffers (including strings) must be as long as the largest buffer you could imagine you'd need. Which also introduces artificial limits.
- bjourne 8y agoC has VLAs https://en.wikipedia.org/wiki/Variable-length_array https://en.wikipedia.org/wiki/Variable-length_array Apparently I suck at linking because this was the thread I was looking for: https://news.ycombinator.com/item?id=18124708 https://news.ycombinator.com/item?id=18124708 As you can see, there is no heap in C so we are free to ignore it.
- kaoD 8y agoI pretty much agree with that thread's replies, not much to add. I didn't know VLAs though (they didn't exist back in the day when I used C). Even though you still have no growable buffers/reallocation (AFAICT), I concede those open up a wider class of complex programs. Thanks for the pointer (pun intended) on VLAs, will check them out. Still no garbage collection though (and I agree with you that neither is Rust's).