Questions tagged [stack]

A stack is a last in, first out (LIFO) abstract data type and data structure. For questions about the assembly call stack, use [stack-memory], [stack-pointer], and/or [stack-frame] instead. For questions about the Haskell build tool, use [haskell-stack] instead. For questions about C++ std::stack, use [stdstack] instead.

stack
Filter by
Sorted by
Tagged with
745 votes
5 answers
246k views

Why is Java Vector (and Stack) class considered obsolete or deprecated?

Why is Java Vector considered a legacy class, obsolete or deprecated? Isn't its use valid when working with concurrency? And if I don't want to manually synchronize objects and just want to use a ...
fjsj's user avatar
  • 11.1k
53 votes
7 answers
20k views

Getting a stack overflow exception when declaring a large array

The following code is generating a stack overflow error for me int main(int argc, char* argv[]) { int sieve[2000000]; return 0; } How do I get around this? I am using Turbo C++ but would ...
Patrick McDonald's user avatar
155 votes
9 answers
236k views

How to increase the Java stack size?

I asked this question to get to know how to increase the runtime call stack size in the JVM. I've got an answer to this, and I've also got many useful answers and comments relevant to how Java handles ...
pts's user avatar
  • 84.9k
10 votes
3 answers
38k views

Implementing back/forward buttons in Swing

I have a quick question. I'm getting a little bit of experience with Swing and the easiest way to do this was to draw up a reasonably big GUI. As part of the GUI, I want to have Forward and Back ...
stephenfin's user avatar
  • 1,457
506 votes
24 answers
198k views

Why is the use of alloca() not considered good practice?

alloca() allocates memory on the stack rather than on the heap, as in the case of malloc(). So, when I return from the routine the memory is freed. So, actually this solves my problem of freeing up ...
Vaibhav's user avatar
  • 5,889
21 votes
9 answers
6k views

Why doesn't my program crash when I write past the end of an array?

Why does the code below work without any crash @ runtime ? And also the size is completely dependent on machine/platform/compiler!!. I can even give upto 200 in a 64-bit machine. how would a ...
vprajan's user avatar
  • 1,147
186 votes
9 answers
173k views

Stack, Static, and Heap in C++

I've searched, but I've not understood very well these three concepts. When do I have to use dynamic allocation (in the heap) and what's its real advantage? What are the problems of static and stack? ...
Hai's user avatar
  • 4,786
371 votes
10 answers
752k views

Stack smashing detected

I am executing my a.out file. After execution the program runs for some time then exits with the message: **** stack smashing detected ***: ./a.out terminated* *======= Backtrace: =========* */lib/...
Biswajyoti Das's user avatar
363 votes
39 answers
312k views

Android: Clear the back stack

In Android I have some activities, let's say A, B, C. In A, I use this code to open B: Intent intent = new Intent(this, B.class); startActivity(intent); In B, I use this code to open C: Intent ...
Martin's user avatar
  • 7,260
548 votes
24 answers
141k views

Which is faster: Stack allocation or Heap allocation

This question may sound fairly elementary, but this is a debate I had with another developer I work with. I was taking care to stack allocate things where I could, instead of heap allocating them. He ...
Adam's user avatar
  • 26.3k
144 votes
2 answers
49k views

Why is stack size in C# exactly 1 MB?

Today's PCs have a large amount of physical RAM but still, the stack size of C# is only 1 MB for 32-bit processes and 4 MB for 64-bit processes (Stack capacity in C#). Why the stack size in CLR is ...
Nikolay Kostov's user avatar
85 votes
12 answers
66k views

Calling delete on variable allocated on the stack

Ignoring programming style and design, is it "safe" to call delete on a variable allocated on the stack? For example: int nAmount; delete &nAmount; or class sample { public: sample();...
unistudent's user avatar
91 votes
2 answers
70k views

Browser Javascript Stack size limit

I am getting some client-side Javascript stack overflow issues specifically in IE browser, this is happening inside a third party library that makes some function calls and for some reason they ...
guilhebl's user avatar
  • 8,690
42 votes
10 answers
27k views

Order of local variable allocation on the stack

Take a look at these two functions: void function1() { int x; int y; int z; int *ret; } void function2() { char buffer1[4]; char buffer2[4]; char buffer3[4]; int *ret;...
David's user avatar
  • 1,131
118 votes
9 answers
54k views

What is the direction of stack growth in most modern systems?

I am preparing some training materials in C and I want my examples to fit the typical stack model. What direction does a C stack grow in Linux, Windows, Mac OSX (PPC and x86), Solaris, and most ...
Uri's user avatar
  • 89.1k
105 votes
15 answers
100k views

Does stack grow upward or downward?

I have this piece of code in C: int q = 10; int s = 5; int a[3]; printf("Address of a: %d\n", (int)a); printf("Address of a[1]: %d\n", (int)&a[1]); printf("Address of ...
user avatar
28 votes
3 answers
88k views

Increase stack size in Linux with setrlimit

reading information about how to increase stack size for a c++ application compiled with gnu, at compilation time, I understood that it can be done with setrlimit at the beginning of the program. ...
asdf's user avatar
  • 2,371
126 votes
10 answers
59k views

Proper stack and heap usage in C++?

I've been programming for a while but It's been mostly Java and C#. I've never actually had to manage memory on my own. I recently began programming in C++ and I'm a little confused as to when I ...
Alexander's user avatar
  • 3,047
78 votes
9 answers
68k views

Global memory management in C++ in stack or heap?

If I declare a data structure globally in a C++ application , does it consume stack memory or heap memory ? For eg struct AAA { .../.../. ../../.. }arr[59652323];
sameer karjatkar's user avatar
50 votes
6 answers
23k views

Stack allocation, padding, and alignment

I've been trying to gain a deeper understanding of how compilers generate machine code, and more specifically how GCC deals with the stack. In doing so I've been writing simple C programs, compiling ...
David's user avatar
  • 1,131
934 votes
33 answers
606k views

How do you implement a Stack and a Queue in JavaScript?

What is the best way to implement a Stack and a Queue in JavaScript? I'm looking to do the shunting-yard algorithm and I'm going to need these data-structures.
KingNestor's user avatar
  • 66.9k
262 votes
11 answers
126k views

What is stack unwinding?

What is stack unwinding? Searched through but couldn't find enlightening answer!
Rajendra Uppal's user avatar
46 votes
9 answers
86k views

Checking available stack size in C

I'm using MinGW with GCC 3.4.5 (mingw-special vista r3). My C application uses a lot of stack so I was wondering is there any way I can tell programatically how much stack is remaining so I can ...
Paul Hargreaves's user avatar
55 votes
10 answers
53k views

Stack overflows from deep recursion in Java?

After some experience with functional languages, I'm starting to use recursion more in Java - But the language seems to have a relatively shallow call stack of about 1000. Is there a way to make the ...
Lucky's user avatar
  • 4,797
120 votes
11 answers
34k views

Why do stacks typically grow downwards?

I know that in the architectures I'm personally familiar with (x86, 6502, etc), the stack typically grows downwards (i.e. every item pushed onto the stack results in a decremented SP, not an ...
Ben Zotto's user avatar
  • 70.6k
58 votes
4 answers
42k views

Stack capacity in C#

Could someone tell me what the stack capacity is in C#. I am trying to form a 3D mesh closed object using an array of 30,000 items.
user avatar
52 votes
6 answers
79k views

How to evaluate an infix expression in just one scan using stacks?

I want to know if there is a way to solve infix expressions in a single pass using 2 stacks? The stacks can be one for operator and the other for operands... The standard way to solve by shunt-yard ...
nikoo28's user avatar
  • 2,961
48 votes
4 answers
64k views

Setting stacksize in a python script

I am converting a csh script to a python script. The script calls a memory-intensive executable which requires a very large stack, so the csh script sets the stacksize to unlimited: limit stacksize ...
marshall.ward's user avatar
34 votes
4 answers
27k views

How to change stack size for a .NET program?

I have a program that does recursive calls for 2 billion times and the stack overflow. I make changes, and then it still need 40K recursive calls. So I need probably several MB stack memory. I heard ...
Frank's user avatar
  • 7,435
30 votes
6 answers
39k views

Difference between char *str="STRING" and char str[] = "STRING"?

While coding a simple function to remove a particular character from a string, I fell on this strange issue: void str_remove_chars( char *str, char to_remove) { if(str && to_remove) { ...
Gui13's user avatar
  • 13.3k
465 votes
20 answers
391k views

How to implement a queue using two stacks?

Suppose we have two stacks and no other temporary variable. Is to possible to "construct" a queue data structure using only the two stacks?
Nitin's user avatar
  • 15.3k
87 votes
2 answers
130k views

What is the default stack size, can it grow, how does it work with garbage collection?

I understand that each thread has its own stack. Primitive types and references are kept on the stack, and that no object is kept on the stack. My questions are: How much can a stack grow? (like ...
codingenious's user avatar
  • 8,495
10 votes
4 answers
15k views

Declare large array on Stack

I am using Dev C++ to write a simulation program. For it, I need to declare a single dimensional array with the data type double. It contains 4200000 elements - like double n[4200000]. The compiler ...
Aniruddh Jammoria's user avatar
72 votes
2 answers
93k views

When and how to use GCC's stack protection feature?

I have enabled the -Wstack-protector warning when compiling the project I'm working on (a commercial multi-platform C++ game engine, compiling on Mac OS X 10.6 with GCC 4.2). This flag warns about ...
Guillaume's user avatar
  • 5,011
64 votes
4 answers
15k views

"enter" vs "push ebp; mov ebp, esp; sub esp, imm" and "leave" vs "mov esp, ebp; pop ebp"

What is the difference between the enter and push ebp mov ebp, esp sub esp, imm instructions? Is there a performance difference? If so, which is faster and why do compilers always use the latter? ...
小太郎's user avatar
  • 5,570
58 votes
4 answers
47k views

Why does the stack address grow towards decreasing memory addresses?

I read in text books that the stack grows by decreasing memory address; that is, from higher address to lower address. It may be a bad question, but I didn't get the concept right. Can you explain?
Jestin Joy's user avatar
  • 3,791
55 votes
4 answers
43k views

Stack with find-min/find-max more efficient than O(n)?

I am interested in creating a Java data structure similar to a stack that supports the following operations as efficiently as possible: Push, which adds a new element atop the stack, Pop, which ...
Techkriti's user avatar
  • 551
52 votes
5 answers
44k views

How to determine maximum stack usage?

What methods are available for determining the optimum stack size for embedded/memory constrained system? If it's too big then memory is wasted that could be used elsewhere. However, if it is too ...
Judge Maygarden's user avatar
126 votes
31 answers
118k views

design a stack such that getMinimum( ) should be O(1)

This is an interview question. You need to design a stack which holds an integer value such that getMinimum() function should return the minimum element in the stack. For example: case #1 5 ← TOP 1 ...
Ganesh M's user avatar
  • 3,696
3 votes
5 answers
585 views

Why do I get a segfault in C from declaring a large array on the stack?

I get a segfault from this line of code: int fatblob[1820][286][5]; Why is that?
Claudiu's user avatar
  • 227k
250 votes
8 answers
128k views

Why should I use Deque over Stack?

I need a Stack data structure for my use case. I should be able to push items into the data structure and I only want to retrieve the last item from the Stack. The JavaDoc for Stack says : A more ...
Geek's user avatar
  • 26.8k
160 votes
9 answers
12k views

In C, do braces act as a stack frame?

If I create a variable within a new set of curly braces, is that variable popped off the stack on the closing brace, or does it hang out until the end of the function? For example: void foo() { ...
Claudiu's user avatar
  • 227k
146 votes
12 answers
127k views

Android: Clear Activity Stack

I'm having several activities in my application. and flow is very complicated. When I click the Logout application navigates to login Screen and from there user can exit by cancel button (calling ...
Jaya Mayu's user avatar
  • 17.2k
144 votes
9 answers
113k views

Java / Android - How to print out a full stack trace?

In Android (Java) how do I print out a full stack trace? If my application crashes from nullPointerException or something, it prints out a (almost) full stack trace like so: java.io.IOException: ...
Jake Wilson's user avatar
  • 89.9k
133 votes
8 answers
75k views

Do threads have a distinct heap?

As far as I know each thread gets a distinct stack when the thread is created by the operating system. I wonder if each thread has a heap distinct to itself also?
user avatar
65 votes
8 answers
12k views

How does a stackless language work?

I've heard of stackless languages. However I don't have any idea how such a language would be implemented. Can someone explain?
rlbond's user avatar
  • 66.7k
47 votes
6 answers
22k views

Stack-buffer based STL allocator?

I was wondering if it practicable to have an C++ standard library compliant allocator that uses a (fixed sized) buffer that lives on the stack. Somehow, it seems this question has not been ask this ...
Martin Ba's user avatar
  • 38.1k
19 votes
4 answers
23k views

Observable Stack and Queue

I'm looking for an INotifyCollectionChanged implementation of Stack and Queue. I could roll my own but I don't want to reinvent the wheel.
Goran's user avatar
  • 6,816
39 votes
4 answers
15k views

What is the difference between a segmentation fault and a stack overflow?

For example when we call say, a recursive function, the successive calls are stored in the stack. However, due to an error if it goes on infinitely the error is 'Segmentation fault' (as seen on GCC). ...
AruniRC's user avatar
  • 5,120
39 votes
31 answers
211k views

Parenthesis/Brackets Matching using Stack algorithm

For example if the parenthesis/brackets is matching in the following: ({}) (()){}() () and so on but if the parenthesis/brackets is not matching it should return false, eg: {} ({}( ){}) (() and so ...
Shuvo0o's user avatar
  • 489

1
2 3 4 5
18