Originally posted by loinburger
It unnecessarily bounds the size of your stack and introduces a lot of internal fragmentation.
A linked list of small (say, 4-12 elements) arrays reduces memory overhead, has minimal internal fragmentation, improves cache performance, and lets you do some loop transformations that aren't possible with a simple linked list (e.g., loop unrolling). Implementing the stack as one massive array doesn't gain you much over the linked list of arrays, and its disadvantages greatly outweigh its negligible advantages.
Yes, it's a simple way to code a stack. No, you should never code a stack this way.
It unnecessarily bounds the size of your stack and introduces a lot of internal fragmentation.
A linked list of small (say, 4-12 elements) arrays reduces memory overhead, has minimal internal fragmentation, improves cache performance, and lets you do some loop transformations that aren't possible with a simple linked list (e.g., loop unrolling). Implementing the stack as one massive array doesn't gain you much over the linked list of arrays, and its disadvantages greatly outweigh its negligible advantages.
Yes, it's a simple way to code a stack. No, you should never code a stack this way.
If you implement the stack for an application on a PC with 1 or more gigs of memory, a swap file and a powerful OS for the memory management, which reboots at least once a day, so that memory leaks aren't all that dangerous, you can make your stack open-ended with dynamic allocation.
If however you implement the stack as part of the firmware of an embedded system with merely a few KB or MB RAM, without mass storage for a swap file, and which is supposed to run several months or even years without reboot, you'd better implement it the way UR said, avoiding per-element dynamic allocation. And you'll make yourself carefully sure, that this hard limit is never exceeded.
Comment