Why are stack overflows still a problem?
I’ve never personally encountered a stack overflow that wasn’t caused by infinite recursion. In these cases, a dynamic stack size wouldn’t help, it would just take a little longer to run out of memory.
I’ve never personally encountered a stack overflow that wasn’t caused by infinite recursion. In these cases, a dynamic stack size wouldn’t help, it would just take a little longer to run out of memory.
There’s actually a Stack class: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.html If you don’t want to use that, the LinkedList class (http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html) has addFirst and addLast and removeFirst and removeLast methods, making it perfect for use as a stack or queue class.
First, to your original question, just be aware that if you’re working with very long strings, you don’t want to be making exact copies minus a single letter each time you make a function call. So you should favor using indexes or verify that your language of choice isn’t making copies behind the scenes. Second, … Read more
On Windows a stack overflow exception will be generated. The following windows code illustrates this: #include <stdio.h> #include <windows.h> void StackOverFlow() { CONTEXT context; // we are interested control registers context.ContextFlags = CONTEXT_CONTROL; // get the details GetThreadContext(GetCurrentThread(), &context); // print the stack pointer printf(“Esp: %X\n”, context.Esp); // this will eventually overflow the stack StackOverFlow(); … Read more
subList(list.size() – N, list.size()).clear() is the recommended way to remove the last N elements. Indeed, the Javadoc for subList specifically recommends this idiom: This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing … Read more
See Bug ID 4475301 : RFE: java.util.Stack.iterator() iterates the wrong way. This behavior is by (bad) design. Java’s built-in Stack iterator methods are inherited from other classes, so they don’t behave as you’d expect.
The standard library containers separate top() and pop(): top() returns a reference to the top element, and pop() removes the top element. (And similarly for back()/pop_back() etc.). There’s a good reason for this separation, and not have pop remove the top element and return it: One guiding principle of C++ is that you don’t pay … Read more
The getrusage function gets you the current usage . (see man getrusage). The getrlimit in Linux would help fetching the stack size with the RLIMIT_STACK parameter. #include <sys/resource.h> int main (void) { struct rlimit limit; getrlimit (RLIMIT_STACK, &limit); printf (“\nStack Limit = %ld and %ld max\n”, limit.rlim_cur, limit.rlim_max); } Please give a look at man … Read more
I have good experience with the following code. It doesn’t require any special user permissions: import resource, sys resource.setrlimit(resource.RLIMIT_STACK, (2**29,-1)) sys.setrecursionlimit(10**6) It does however not seem to work with pypy. If resource.setrlimit doesn’t work, you can also try using threads: sys.setrecursionlimit(10**6) import threading threading.stack_size(2**26) threading.Thread(target=main).start()
It’s definitely possible to create a fully C++11/C++14 conforming stack allocator*. But you need to consider some of the ramifications about the implementation and the semantics of stack allocation and how they interact with standard containers. Here’s a fully C++11/C++14 conforming stack allocator (also hosted on my github): #include <functional> #include <memory> template <class T, … Read more