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
1
vote
3 answers

g++ compiler hints to allocate on stack

Are there any methods to give the compiler hints that some objects may have a more static behaviour, and allocate things on the stack instead of heap ? For example a string object might have a kind of a constant size inside some functions. I'm…
Catalin Vasile
  • 367
  • 5
  • 17
0
votes
1 answer

C++ Create a class instance and pointer to it in one line

There is a one line syntax to create an instance and pointer to it, in the heap allocation. Is there one line syntax for the same purpose but with stack allocation? #include class Base {}; int main() { //Base* ptr = new Base(); //…
Nick
  • 11
  • 1
0
votes
2 answers

Problems that may arise when initializing arrays on stack inside a function scope with an N size_t parameter?

Say for example I have a function that takes some argument and a size_t length to initialize an array on stack inside a function. Considering the following: Strictly the length can only be on the range of 1 to 30 (using a fixed max buffer length of…
0
votes
0 answers

Factory pattern for stack allocated objects in c++

I'm a beginner c++ programmer and I would like to know whether it is possible to create stack-allocated objects with a factory design pattern like in this code. ''' class IInterface { public: virtual ~IInterface() = default; }; class Car :…
Pasan
  • 1
0
votes
0 answers

realloc stack allocated char (*)[n]

I have a function that does some reallocating like this: void str_replace(char** str, const char* a, const char* b) { *str = realloc(*str, 100); } and I call that function in main using: char test[] = "some test string"; str_replace(&test,…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

Class type for stack allocation. Why the address for both ID instances are the same?

class ID { public: ID(const std::string& name) : name_(name) {} // explicit copy constructor as my first solution but gave me same address ID(const ID& other) { name_ = other.getName(); } std::string getName() const …
0
votes
1 answer

Allocation-free enumeration and processing

I would like to solve the massive allocation costs of a c# application. The application itself can be represented by the TickUser class at the bottom, and I'd like to know how to implement the TickStream object and the DoWork and ProcessTick methods…
bboyle1234
  • 4,859
  • 2
  • 24
  • 29
0
votes
1 answer

Deleting objects with heap members

I am working on a bitset implementation. The bitset uses an array of unsigned long long to store the bits. class bitset{ typedef unsigned long long uint64; uint64* bits; ... } Since I need this bitset to store a large about of data, I…
elitk19
  • 31
  • 4
0
votes
0 answers

static allocation and stack allocation in compiler design

I am not clear about was static and stack allocation are? Is static allocation static and stack allocation dynamic? Then where does heap allocation belong? How is activation record related to this? I thought activation record is a conceptual thing…
afsara_ben
  • 542
  • 1
  • 11
  • 30
0
votes
3 answers

How compiler manages runtime stack?

There are lots of questions asked about stack & heap on this site. But i want to know about how compiler manages stack actually? Is the stack based allocation is decided at runtime or compile time? Consider following example: #include…
Destructor
  • 14,123
  • 11
  • 61
  • 126
-1
votes
1 answer

Marshal size const array

I'm trying to have a stack allocated array inside a struct. Well the pointer I mean. But I'd like the allocation to be done without extra code because I know the size when I write the code (I don't want to do a bunch of new when I create my…
Mathieu Van Nevel
  • 1,428
  • 1
  • 10
  • 26
-2
votes
1 answer

How can I load all entries of a Vec of arbitrary length onto the stack?

I am currently working with vectors and trying to ensure I have what is essentially an array of my vector on the stack. I cannot call Vec::into_boxed_slice since I am dynamically allocating space in my Vec. Is this at all possible? Having read the…
Bots Fab
  • 139
  • 1
  • 9
1
2