Questions tagged [stack-memory]

Questions regarding process stack memory. Generally, stack memory is sequentially allocated, scoped to successive function calls, and normally referenced by the CPU using a stack pointer register.

997 questions
243
votes
6 answers

Stack vs heap allocation of structs in Go, and how they relate to garbage collection

I'm experiencing a bit of cognitive dissonance between C-style stack-based programming, where automatic variables live on the stack and allocated memory lives on the heap, and Python-style stack-based-programming, where the only thing that lives on…
Joe
  • 46,419
  • 33
  • 155
  • 245
229
votes
8 answers

What are the dangers when creating a thread with a stack size of 50x the default?

I'm currently working on a very performance critical program and one path I decided to explore that may help reduce resource consumption was increasing my worker threads' stack size so I can move most of the data (float[]s) that I'll be accesing…
Sam
  • 7,252
  • 16
  • 46
  • 65
225
votes
5 answers

When vectors are allocated, do they use memory on the heap or the stack?

Are all of the following statements true? vector vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack vector *vect = new vector; //allocates vect on heap and each of the Type will…
Phelodas
  • 3,853
  • 5
  • 25
  • 30
215
votes
6 answers

How are multi-dimensional arrays formatted in memory?

In C, I know I can dynamically allocate a two-dimensional array on the heap using the following code: int** someNumbers = malloc(arrayRows*sizeof(int*)); for (i = 0; i < arrayRows; i++) { someNumbers[i] =…
Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
154
votes
8 answers

Arrays, heap and stack and value types

int[] myIntegers; myIntegers = new int[100]; In the above code, is new int[100] generating the array on the heap? From what I've read on CLR via c#, the answer is yes. But what I can't understand, is what happens to the actual int's inside the…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
151
votes
7 answers

C/C++ maximum stack size of program on mainstream OSes

I want to do DFS on a 100 X 100 array. (Say elements of array represents graph nodes) So assuming worst case, depth of recursive function calls can go upto 10000 with each call taking upto say 20 bytes. So is it feasible means is there a possibility…
avd
  • 13,993
  • 32
  • 78
  • 99
144
votes
5 answers

What is the function of the push / pop instructions used on registers in x86 assembly?

When reading about assembler I often come across people writing that they push a certain register of the processor and pop it again later to restore it's previous state. How can you push a register? Where is it pushed on? Why is this needed? Does…
Ars emble
  • 1,459
  • 2
  • 10
  • 4
135
votes
3 answers

kernel stack and user space stack

What's the difference between kernel stack and user stack? Why kernel stack is used? If a local variable is declared in an ISR, where it will be stored? Does each process has its own kernel stack? Then how the process coordinates between both these…
jkv
  • 1,627
  • 4
  • 12
  • 14
104
votes
7 answers

Is accessing data in the heap faster than from the stack?

I know this sounds like a general question and I've seen many similar questions (both here and on the web) but none of them are really like my dilemma. Say I have this code: void GetSomeData(char* buffer) { // put some data in buffer } int…
conectionist
  • 2,694
  • 6
  • 28
  • 50
100
votes
17 answers

How does the stack work in assembly language?

I'm currently trying to understand how the stack works, so I've decided teach myself some assembly language, I'm using this book: http://savannah.nongnu.org/projects/pgubook/ I'm using Gas and doing my development on Linux Mint. I'm a bit confused…
handles
  • 7,639
  • 17
  • 63
  • 85
97
votes
6 answers

Is a Java array of primitives stored in stack or heap?

I have an array declaration like this: int a[]; Here a is an array of primitive int type. Where is this array stored? Is it stored on heap or stack? This is a primitve type int, all primitive types are not stored on heap.
user241924
  • 4,340
  • 7
  • 27
  • 25
89
votes
9 answers

Memory allocation: Stack vs Heap?

I am getting confused with memory allocation basics between Stack vs Heap. As per the standard definition (things which everybody says), all Value Types will get allocated onto a Stack and Reference Types will go into the Heap. Now consider the…
Mrinal
  • 941
  • 1
  • 7
  • 6
76
votes
4 answers

Swift stack and heap understanding

I want to understand what is stored in the stack and heap in swift. I have a rough estimation: Everything that you print and the memory address appears not the values, those are stored in the stack, and what is printed out as values, those are on…
Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
76
votes
23 answers

Why not use pointers for everything in C++?

Suppose that I define some class: class Pixel { public: Pixel(){ x=0; y=0;}; int x; int y; } Then write some code using it. Why would I do the following? Pixel p; p.x = 2; p.y = 5; Coming from a Java world I always…
Eric
  • 19,525
  • 19
  • 84
  • 147
70
votes
7 answers

Object creation on the stack/heap?

The following code creates an object on the stack: Object o; When creating an object on the heap we can use: Object* o; o = new Object(); rather than: Object* o = new Object(); When we split the heap object-creation over two lines and call the…
user997112
  • 29,025
  • 43
  • 182
  • 361
1
2 3
66 67