32 questions
6
votes
1
answer
241
views
How do I create and use a stack-allocated struct ending with a C99 flexible array member in C++ without undefined behaviour?
I'm calling a 3rd-party C99 library from modern C++. The API contains a struct that ends with a flexible array member. To allocate the struct on the stack I think I need a byte buffer then cast it to ...
1
vote
1
answer
68
views
Does Cython do stack allocation of final extension types? Does it do allocation inline of these types in other extension types? What about C structs?
Consider following Cython code:
import cython
@cython.final
cdef class Fish:
cdef double weight
cdef class Aquarium:
cdef Fish fish
cpdef double fish_weight(Fish fish):
return fish.weight
...
1
vote
1
answer
75
views
Managing the Lifecycle of re2::RE2 Objects with Cached and Stack-Allocated Options
I have a function re02_match(regexp, use_cache, ...) where I need to handle re2::RE2 objects differently based on whether a cached object should be used (use_cache = true) or a stack-allocated object ...
1
vote
1
answer
52
views
About reusing parent process' stack in child processes
I am reading Michael Kerrisk's "Namespaces in operation" series (as I want to implement a container by myself in Linux), and I found myself wondering about something:
In one of Michael's ...
1
vote
0
answers
145
views
Do moves in rust always copy the stack allocation?
Lets say I have a large, non-Copy, stack allocated data structure, and then I move it a bunch of times:
struct Foo(u8);
fn main() {
// A big (8MiB) stack allocation
let big_stack_data: [Foo; ...
0
votes
1
answer
807
views
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 <iostream>
class ...
0
votes
2
answers
88
views
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 ...
7
votes
1
answer
384
views
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);
}
...
0
votes
0
answers
29
views
Factory pattern for stack allocated objects in c++ [duplicate]
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:
...
1
vote
0
answers
145
views
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 ...
0
votes
0
answers
41
views
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 ...
1
vote
2
answers
278
views
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 ...
-2
votes
1
answer
283
views
How can I load all entries of a Vec<T> 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 ...
4
votes
2
answers
291
views
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.
0
votes
1
answer
48
views
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_ =...
1
vote
0
answers
186
views
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 ...
0
votes
1
answer
459
views
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 ...
14
votes
1
answer
560
views
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 ...
5
votes
0
answers
1k
views
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 ...
0
votes
1
answer
90
views
Deleting objects with heap members [duplicate]
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 ...
-1
votes
1
answer
801
views
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 ...
4
votes
1
answer
347
views
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 ...
0
votes
0
answers
566
views
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 ...
1
vote
1
answer
580
views
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 ...
2
votes
1
answer
1k
views
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 ...
20
votes
1
answer
4k
views
When can Hotspot allocate objects on the stack? [duplicate]
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 ...
38
votes
3
answers
3k
views
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 ...
3
votes
0
answers
499
views
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) {
...
0
votes
3
answers
542
views
How compiler manages runtime stack? [duplicate]
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?...
1
vote
3
answers
209
views
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 ...
10
votes
7
answers
3k
views
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 ...
30
votes
3
answers
38k
views
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 ...