Questions tagged [stack-allocation]

It's a way to allocate memory in the computer.

It's a way to allocate memory in the computer.

Stacks in computing architectures are regions of memory where data is added or removed in a last-in-first-out manner.

In most modern computer systems, each thread has a reserved region of memory referred to as its stack. When a function executes, it may add some of its state data to the top of the stack; when the function exits it is responsible for removing that data from the stack. At a minimum, a thread's stack is used to store the location of function calls in order to allow return statements to return to the correct location, but programmers may further choose to explicitly use the stack. If a region of memory lies on the thread's stack, that memory is said to have been allocated on the stack.

https://en.wikipedia.org/wiki/Stack-based_memory_allocation

27 questions
36
votes
3 answers

C++ replacement for C99 VLAs (goal: preserve performance)

I am porting some C99 code that makes heavy use of variable length arrays (VLA) to C++. I replaced the VLAs (stack allocation) with an array class that allocates memory on the heap. The performance hit was huge, a slowdown of a factor of 3.2 (see…
Szabolcs
  • 24,728
  • 9
  • 85
  • 174
28
votes
3 answers

Variable-length std::array like

As my usually used C++ compilers allow variable-length arrays (e.g. arrays depending on runtime size), I wonder if there is something like std::array with variable size? Of course std::vector is of variable size, but it allocates on heap, and…
dronus
  • 10,774
  • 8
  • 54
  • 80
19
votes
1 answer

When can Hotspot allocate objects on the stack?

Since somewhere around Java 6, the Hotspot JVM can do escape analysis and allocate non-escaping objects on the stack instead of on the garbage collected heap. This results in a speedup of the generated code and reduces pressure on the garbage…
JanKanis
  • 6,346
  • 5
  • 38
  • 42
14
votes
1 answer

Why is stack memory allocated when it is not used?

Consider the following example: struct vector { int size() const; bool empty() const; }; bool vector::empty() const { return size() == 0; } The generated assembly code for vector::empty (by clang, with optimizations): push rax call…
Dr. Gut
  • 2,053
  • 7
  • 26
10
votes
7 answers

How to implement a string that solely allocates on the stack

In a project about a decade ago, we found that std::vector's dynamic allocations caused a serious performance drain. In this case it was many small vectors allocated, so the quick solution was to write a vector-like class wrapping around a…
sbi
  • 219,715
  • 46
  • 258
  • 445
7
votes
1 answer

Is there a way for force gcc to free the space from the stack when variables go out of scope

I have the following piece of code: extern void func1(char *array); extern void func2(char *array); void myfunction(void) { if (somecondition) { char var2[256]; func2(var2); } if (someothercondition) { { char…
Louis Caron
  • 1,043
  • 1
  • 11
  • 17
4
votes
2 answers

How come the stack cannot be increased during runtime in most operating system?

Is it to avoid fragmentation? Or some other reason? A set lifetime for a memory allocation is a pretty useful construct, compared to malloc() which has a manual lifetime.
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
4
votes
0 answers

Escape Analysis and stack allocation optimization improvements in JAVA 9 and beyond

Are the rules for stack allocation optimization less strict for HotSpot Java 9-13 ? In Java 7 & Java 8 HotSpot stack allocation of objects (due to JVM optimization known as scalar object replacement) is possible but to achieve garbage free…
4
votes
1 answer

How can I compel the MSVC compiler to elide stack-allocation of large temporary objects?

This question is not a duplicate of this one or other similar questions. This question is about clearing a struct after it has been initialized and used already. Update After reading the first few of your comments I would like to clarify my…
user23573
  • 2,479
  • 1
  • 17
  • 36
3
votes
1 answer

Explicitly stack-allocated data

I think in most implementations of Common Lisp cons cells are generally/always heap allocated (see Why is consing in Lisp slow?) Common Lisp does provide a facility for returning multiple values from a function (using values when returning and…
Greg Nisbet
  • 6,710
  • 3
  • 25
  • 65
3
votes
0 answers

Stack allocated immutable arrays

I'm doing extensive computations in f# on short arrays of uint64; I'd like to stack allocate them to avoid the garbage collector running. In C++, I'd do this: int search(int n, uint64_t* data) { while ( /* something */ ) { // ... do…
Søren Debois
  • 5,598
  • 26
  • 48
1
vote
0 answers

What does an out-of-line call mean?

I was reading the JEP doc about Value Objects in Java and came across a line in the motivation section in context of scalarization of objects that read like this: There are optimizations which can eliminate object allocation in some regions of…
Shankha057
  • 1,296
  • 1
  • 20
  • 38
1
vote
2 answers

How can I replace an std::string member with a non-heap limited-size string?

I have a codebase with some ubiquitous data structure; and said structure has an std::string member. Now, for reasons, I want this codebase to work when std::string is unavailable, and in fact with no dynamic allocation of memory (at least not the…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1
vote
0 answers

Does a stack allocation through _malloca trigger an alloc hook set through _CrtSetAllocHook

For a realtime audio signal processing application, we want to make sure that no heap memory allocations are performed from within the realtime threads. As an internal debugging tool used during development, we set up an heap allocation hook…
PluginPenguin
  • 1,576
  • 11
  • 25
1
vote
1 answer

How can I emulate a stack frame in C++?

I am writing a container that uses alloca internally to allocate data on the stack. Risks of using alloca aside, assume that I must use it for the domain I am in (it's partly a learning exercise around alloca and partly to investigate possible…
OMGtechy
  • 7,935
  • 8
  • 48
  • 83
1
2