Current time: 19 May 2024, 07:06 AM



Stack Overflow (C++)
Offline SmG xBr4v3x

Senior Member
Senior Member
******
11 Years of Service
Posts: 374
Threads: 43
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Mar 2013
Reputation: 9
Location: Stormwind
Discord: LukeWasWrong Admin#5075
Hand-held/Mobile GamerSuper ComputerBronze MedalOverwatch
#2
Re: Stack Overflow (C++)

The most common cause of stack overflow is excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack.

An example of infinite recursion in C.

Code:
int foo()
{
     return foo();
}
The function foo, when it is invoked, continues to invoke itself, allocating additional space on the stack each time, until the stack overflows resulting in a segmentation fault. However, some compilers implement tail-call optimization, allowing infinite recursion of a specific sort—tail recursion—to occur without stack overflow. This works because tail-recursion calls do not take up additional stack space.

C compiler options will effectively enable tail-call optimization; compiling the above simple program using gcc with
Code:
-O1
will result in a segmentation fault, but not when using
Code:
-O2
or
Code:
-O3
, since these optimization levels imply the
Code:
-foptimize-sibling-calls
compiler option. Other languages, such as Scheme, require all implementations to include tail-recursion as part of the language standard.

========================================================

The other major cause of a stack overflow results from an attempt to allocate more memory on the stack than will fit, for example by creating local array variables that are too large. For this reason some authors recommend that arrays larger than a few kilobytes should be allocated dynamically instead of as a local variable.

An example of a very large stack variable in C:

Code:
int foo()
{
     double x[1048576];
}
The declared array consumes 8 mebibytes of data (assuming each double is 8 bytes); if this is more memory than is available on the stack (as set by thread creation parameters or operating system limits), a stack overflow will occur.

Stack overflows are made worse by anything that reduces the effective stack size of a given program. For example, the same program being run without multiple threads might work fine, but as soon as multi-threading is enabled the program will crash. This is because most programs with threads have less stack space per thread than a program with no threading support. Because kernels are generally multi-threaded, people new to kernel development are usually discouraged from using recursive algorithms or large stack buffers.

=======================================================


Does that help you understand a little more as to why it may be occurring?
You Gotta be Brave to Embark On The Trail To Success
==============================================



==============================================
|||OLD|||
[Image: 5351139349.png]
|||NEW|||
[Image: 6151473987.png]




==============================================
24 May 2016, 02:03 PM
Find Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)
SmG Gaming, © 2010-2024
Theme By: «SmG» Cloud
Edited by: «SmG» Wires