Current time: 25 Apr 2024, 02:11 PM



Stack Overflow (C++)
Away SmG FlasH™

Senior Member
Senior Member
******
11 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#1
Stack Overflow (C++)

So my assignment was to make a console application using c++ for 100 customers. I use a main menu and take the input and redirect it to the specific function. The problem is, I can only register upto 15~ users. Once I try to register beyond that, it gives me a stack overflow error. I'm guessing this is with memory allocation or something. But I have no clue.

Any idea on how to fix it?

I basically obtain the user's details and store them in variables. One user is registered at a time, once registration is done, it is redirected to the menu and you can register again.

Also my when I take the input from the user from the menu (consisting of 11 functions) I use an integer variable to store the value. Therefore if a string/char is entered, it goes into an infinite loop.

Main priority is the stack overflow error tho, that's like the main requirement xD

P.S: I can pm my code if necessary.

Thanx in advance :)
[Image: SPOILER_FlasH-1.gif]
24 May 2016, 01:10 PM
Find Reply
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
Offline SmG Dragonrage

Royal Dragon
Senior Member
******
11 Years of Service
Posts: 4,018
Threads: 206
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Aug 2012
Reputation: 38
Location: Atlantis
Gavel of TrinityLeague of LegendsProgrammerCoolCrystal DonatorLegendary
#3
Re: Stack Overflow (C++)

yea recursion, while easy to implement, once it has to recurse a bunch of times, it will overflow your stack, so your best bet is to do it iteratively instead
[Image: mla7mw8hs3u0g9czg.jpg?size_id=5]
Do not meddle in the affairs of Dragons, for you are crunchy and taste good with ketchup!
24 May 2016, 02:11 PM
Website Find Reply
Offline Leaf

Registered User
Registered
11 Years of Service
Posts: 397
Threads: 24
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Jun 2012
Reputation: 0
Programmer
#4
Re: Stack Overflow (C++)

Whats your code look like? it's be easier to see where you went wrong and explain the problem from an example. Feel free to pm me code or use pastebin if you need any help.
24 May 2016, 02:32 PM
Find Reply
Away SmG FlasH™

Senior Member
Senior Member
******
11 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#5
Re: Stack Overflow (C++)

@Brave: Yes, thanx, it did help in understanding it better. I did not understand it completely, but yes helped a lot.

@Dragon: I haven't yet learnt what those terms mean xD

@Leaf: Link to the code: <!-- m --><a class="postlink" href="http://pastebin.com/phiPN9PU">http://pastebin.com/phiPN9PU</a><!-- m -->

Basically I have two arrays each of size 100, One for the NIC and one for the CID. Other than that its just string and int variables.

The code I've given above is a scaled down version. So there might be a few bugs, but It should do the job.
[Image: SPOILER_FlasH-1.gif]
25 May 2016, 12:40 AM
Find Reply
Offline Leaf

Registered User
Registered
11 Years of Service
Posts: 397
Threads: 24
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Jun 2012
Reputation: 0
Programmer
#6
Re: Stack Overflow (C++)

I can name a number of flaws/bugs with the code you linked, could you post the full version so that I know exactly whats going on?
26 May 2016, 02:01 AM
Find Reply
Away SmG FlasH™

Senior Member
Senior Member
******
11 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#7
Re: Stack Overflow (C++)

Full Code: <!-- m --><a class="postlink" href="http://pastebin.com/m0vr68tV">http://pastebin.com/m0vr68tV</a><!-- m -->
[Image: SPOILER_FlasH-1.gif]
26 May 2016, 03:12 AM
Find Reply
Offline Leaf

Registered User
Registered
11 Years of Service
Posts: 397
Threads: 24
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Jun 2012
Reputation: 0
Programmer
#8
Re: Stack Overflow (C++)

Thanks ill take a look tonight when I get on my pc
26 May 2016, 11:10 AM
Find Reply
Away SmG FlasH™

Senior Member
Senior Member
******
11 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#9
Re: Stack Overflow (C++)

SmG Leaf Wrote:Thanks ill take a look tonight when I get on my pc

I told my lecturer about the prob, and he said its not an issue, as long as it can register around 15 customers. But I'd like to learn why it happens and how to fix it, so if you have free time please do teach me xD
[Image: SPOILER_FlasH-1.gif]
26 May 2016, 12:15 PM
Find Reply


Forum Jump:


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