Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 42643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:22:13+00:00 2026-05-10T15:22:13+00:00

How does a stack overflow occur and what are the ways to make sure

  • 0

How does a stack overflow occur and what are the ways to make sure it doesn’t happen, or ways to prevent one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:22:13+00:00Added an answer on May 10, 2026 at 3:22 pm

    Stack

    A stack, in this context, is the last in, first out buffer you place data while your program runs. Last in, first out (LIFO) means that the last thing you put in is always the first thing you get back out – if you push 2 items on the stack, ‘A’ and then ‘B’, then the first thing you pop off the stack will be ‘B’, and the next thing is ‘A’.

    When you call a function in your code, the next instruction after the function call is stored on the stack, and any storage space that might be overwritten by the function call. The function you call might use up more stack for its own local variables. When it’s done, it frees up the local variable stack space it used, then returns to the previous function.

    Stack overflow

    A stack overflow is when you’ve used up more memory for the stack than your program was supposed to use. In embedded systems you might only have 256 bytes for the stack, and if each function takes up 32 bytes then you can only have function calls 8 deep – function 1 calls function 2 who calls function 3 who calls function 4 …. who calls function 8 who calls function 9, but function 9 overwrites memory outside the stack. This might overwrite memory, code, etc.

    Many programmers make this mistake by calling function A that then calls function B, that then calls function C, that then calls function A. It might work most of the time, but just once the wrong input will cause it to go in that circle forever until the computer recognizes that the stack is overblown.

    Recursive functions are also a cause for this, but if you’re writing recursively (ie, your function calls itself) then you need to be aware of this and use static/global variables to prevent infinite recursion.

    Generally, the OS and the programming language you’re using manage the stack, and it’s out of your hands. You should look at your call graph (a tree structure that shows from your main what each function calls) to see how deep your function calls go, and to detect cycles and recursion that are not intended. Intentional cycles and recursion need to be artificially checked to error out if they call each other too many times.

    Beyond good programming practices, static and dynamic testing, there’s not much you can do on these high level systems.

    Embedded systems

    In the embedded world, especially in high reliability code (automotive, aircraft, space) you do extensive code reviews and checking, but you also do the following:

    • Disallow recursion and cycles – enforced by policy and testing
    • Keep code and stack far apart (code in flash, stack in RAM, and never the twain shall meet)
    • Place guard bands around the stack – empty area of memory that you fill with a magic number (usually a software interrupt instruction, but there are many options here), and hundreds or thousands of times a second you look at the guard bands to make sure they haven’t been overwritten.
    • Use memory protection (ie, no execute on the stack, no read or write just outside the stack)
    • Interrupts don’t call secondary functions – they set flags, copy data, and let the application take care of processing it (otherwise you might get 8 deep in your function call tree, have an interrupt, and then go out another few functions inside the interrupt, causing the blowout). You have several call trees – one for the main processes, and one for each interrupt. If your interrupts can interrupt each other… well, there be dragons…

    High-level languages and systems

    But in high level languages run on operating systems:

    • Reduce your local variable storage (local variables are stored on the stack – although compilers are pretty smart about this and will sometimes put big locals on the heap if your call tree is shallow)
    • Avoid or strictly limit recursion
    • Don’t break your programs up too far into smaller and smaller functions – even without counting local variables each function call consumes as much as 64 bytes on the stack (32 bit processor, saving half the CPU registers, flags, etc)
    • Keep your call tree shallow (similar to the above statement)

    Web servers

    It depends on the ‘sandbox’ you have whether you can control or even see the stack. Chances are good you can treat web servers as you would any other high level language and operating system – it’s largely out of your hands, but check the language and server stack you’re using. It is possible to blow the stack on your SQL server, for instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Unfortunately, you need to write the code for this yourself.… May 11, 2026 at 11:56 am
  • added an answer First, the profile of the consultant you need is very… May 11, 2026 at 11:56 am
  • added an answer No there is not a Replace method in the BCL… May 11, 2026 at 11:56 am

Related Questions

How does a stack overflow occur and what are the ways to make sure
How does a virtual machine generate native machine code on the fly and execute
I keep wondering how does a debugger work? Particulary the one that can be
See also How does a WCF server inform a WCF client about changes? (Better
This question is probably better phrased: how does running a Java server on a
How does one handle a DateTime with a NOT NULL ? I want to
How does one invoke a groovy method that prints to stdout, appending the output
How does one get a list of running applications as shown in the "Applications"
How does one write a unit test that fails only if a function doesn't
How does sharepoint identify a file type? For example, SharePoint will not allow you

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.