4 ms·
You can't grow a C stack, it's pre-allocated and fixed size. Trying to use too much of a stack - beyond what's initially allocated — is why you're going to segf
by masklinn 8y ago
You can't grow a C stack, it's pre-allocated and fixed size. Trying to use too much of a stack - beyond what's initially allocated — is why you're going to segfault (in the best case), as you'll be traipsing on unallocated addresses and hopefully unmapped pages.
- saagarjha 8y agoI'm using "grow" in the sense of moving the stack pointer down. But yes, you're correct, it's trying to access that memory that will cause a segmentation fault. And to address your first point, I'm sure there's hijinks you can perform with mmap to add a couple more pages if you so desire.
- masklinn 8y ago> I'm sure there's hijinks you can perform with mmap to add a couple more pages if you so desire. I don't think you can do that, but I think you'd just create a bigger stack when spawning your process / thread: the stack allocation is virtual (at least on unices, possibly not on Windows?) so on a 64b system it's unlikely to be very expensive, physical pages will get allocated on-demand as stack use increases. This could probably be tested / benched using pthread: pthread_attr_setstacksize lets you define the (virtual) stack size before spawning the process.
- Someone 8y agoYou can only do that if the new virtual memory address range isn’t used yet for something else. If so, you can almost just as well make the stack larger to start with. Certainly on 64-bit systems with their enormous address spaces, that’s probably the wise thing yo do. Go Can’t do that because it is designed for allowing to run thousands of short-lived threads, even on 32-bit systems.