How do I check what causes a stack overflow?

This is a discussion on How do I check what causes a stack overflow? within the C++ Programming forums, part of the General Programming Boards category; I'm getting a stack overflow error because for large numbers, this code I'm working on allocates too much on the ...

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,276

    How do I check what causes a stack overflow?

    I'm getting a stack overflow error because for large numbers, this code I'm working on allocates too much on the stack.

    Is there a way of tracking stack allocations specifically?

    Will multithreading solve my problem if each thread is doing the static allocations?

    Would I really have to use malloc or new every time I wanted to use memory just to make my code scale to huge numbers?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In C++, we usually use new instead of malloc. I would suggest you using a debugger.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    use std::vectors instead of C arrays allocated on stack
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    What is your compiler again? You can set what the stack size is and avoid trying to allocate too much.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,276
    So here's the thing, I'm using nothing but vectors. And you're right, I should maybe use std::array instead of C-style ones. Well, I really mean that I'm using a lot of vectors and a lot of what I'm already doing is being manually allocated by new() and in one instance to comply with the ISO C standard, malloc() and that's because of a weird constructor glitch. Basically, malloc and free makes valgrind happy but new and delete make it freak out.

    I get that if I don't use
    Code:
    std::vector<int> *vec = new std::vector<int>(size);
    then the vector itself is a static allocation. The element in it are heap allocations but the vector itself would all be on the stack, right?

    I'm using a lot of recursion though too. That might having something to do with it.

    My compiler is also gcc 4.8.1.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,916
    Why are you using new for that snippet? You probably should be just using a "standard" instance and use the constructor to set the size.
    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
       int vec_size = 100;
       std::vector<int> vec(vec_size);
       std::cout << vec.size() << std::endl;
    
    	return 0;
    
    }
    I'm using a lot of recursion though too. That might having something to do with it.
    Most likely. If you run the program with your debugger, the debugger should tell you exactly where it detects the problem and then you can look at the variables at the time of the crash to help determine the actual problem

    If you need more help you'll need to show much more code, the snippet you've shown is nowhere near enough code to help determine your problems.


    Jim

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,276
    Okay, so here's the thing, my code is actually more of project and it is quite long so posting it might not be an option.

    Would this be the time to learn gdb?

  8. #8
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by MutantJohn View Post
    Okay, so here's the thing, my code is actually more of project and it is quite long so posting it might not be an option.

    Would this be the time to learn gdb?
    It's always a good time to learn gdb. If you're on Unix, Linux or OSX also look into valgrind.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Easiest way to detect stack overflow?
    Run the program under a debugger. Let program crash.
    Debugger stops. Open stack backtrace. There. You've identified basically what function(s) caused the overflow because 99% of the time, it's recursion.
    You can then go back and look at variable values to see what caused it to enter this "infinite" recursion.

    As for multi-threaded, don't go there. It's difficult to solve such problems even with a debugger, let alone without.
    Stop using new and malloc unless you really have to (where and why do you use them?). Especially malloc (why are you using it?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,334
    malloc and new aren't going to cause stack overflow. Assuming gcc implements vectors similar to microsoft, only a small header structure is used for a vector, and the data for the vector is allocated / reallocated / freed from the heap. If you use a pointer to a vector and new, then only the pointer is on the stack.

    To help with a stack overflow issue, you could add a global static variable for each recursive function. Each global variable would be incremented before each recursive call and decremented just after each return, then use cout to display function name and recursion level. If the amount of output is slowing things down too much, redirect the output to a text file. Chances are that some recursive process is nesting too many times including one where the condition to stop recursion is never met.

    As mentioned, using a source level debugger would make this much easier. If you're running on windows, microsoft visual c++ express is free and has a good source level debugger as part of it's main IDE.
    Last edited by rcgldr; 09-24-2013 at 03:46 PM.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Well if you can't post the code, then please at least tell us what the code is doing.
    I.e. at a high level what operation is it performing that causes stack overflow.

    E.g. Is it trying to solve a Sudoku? Is it trying to find the shortest path from A to Z? Is it performing a sort of some data?
    Of course the chances that any of the above are a "yes" is very small, but the questions themselves give you an idea of what you need to tell us.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by iMalc View Post
    Well if you can't post the code, then please at least tell us what the code is doing.
    I.e. at a high level what operation is it performing that causes stack overflow.
    Presumably it's his "Delaunay triangulation" code that he posted in another thread.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack overflow....
    By roaan in forum C Programming
    Replies: 16
    Last Post: 09-09-2009, 12:31 PM
  2. Stack overflow
    By alperen1994 in forum C Programming
    Replies: 5
    Last Post: 04-06-2009, 12:11 PM
  3. stack overflow
    By Unregistered in forum C Programming
    Replies: 29
    Last Post: 08-05-2002, 03:57 PM
  4. stack overflow...need help
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 06-18-2002, 02:54 PM
  5. stack overflow?
    By Leeman_s in forum C++ Programming
    Replies: 13
    Last Post: 05-02-2002, 06:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21