-9

How does GCC ensure that The Stack doesn't overflow? Shouldn't it check the Size is less than the MAX it can retain and prompt user accordingly,esp when it is implicitly defined?WIll this not be a great programming paradigm?

2
  • 1
    stackoverflow.com/questions/2630054/… Commented Jan 28, 2013 at 13:16
  • It doesn't, and no it wouldn't be a great programming paradigm. What would you ask the user? And what else can you do, whatever he says, other than abort the computation? Not a real question. Commented Jan 28, 2013 at 23:42

2 Answers 2

8

It doesn't. If you recurse deep enough, you will overflow, and there's nothing the compiler can do about it.


edit: I should point out that at the time I answered this question, the question simply read:

"How does GCC ensure that The Stack doesn't overflow?"

Sign up to request clarification or add additional context in comments.

Comments

1

Linux uses a "guard area". It puts one or more access-protect pages at the end of the stack for each thread.

If the program accesses the guard area, the OS handles the fault. If the thread is already using its max permitted stack then it terminates something (the thread or the whole process, I don't remember which). Otherwise it tries to map memory to the addresses occupied by the guard area for use as stack, and protects a new area beyond the end of the newly-enlarged stack.

Prompting the user isn't really suitable for an OS like Linux, in which many processes are not monitored by a user, and for that matter there may not be any logged-in user at the time the problem arises. So your process just fails. Since it's an all-purpose compiler, gcc doesn't attempt runtime user interaction either.

Other OSes and platforms may or may not have stack guard pages (Windows does). About all gcc really needs to do is to ensure that if the stack is going to be exceeded, it doesn't "miss" the guard page by jumping a long way forward.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.