Questions tagged [static-allocation]

38 questions
66
votes
2 answers

Does std::array<> guarantee allocation on the stack only?

Is std::array (without myself using new) guaranteed to be allocated in the stack rather then the heap by the C++-Standard? To be clear, I do not mean new std::array. I mainly wonder, if the standard library is allowed to use new…
towi
  • 21,587
  • 28
  • 106
  • 187
17
votes
3 answers

Why do Objective-C objects have to be dynamically allocated?

Why do Objective-c objects have to be dynamically allocated? Why do I have to make it a pointer to an object, unlike in C++ I can create them on stack? Thanks.
9
votes
2 answers

Possible to create statically allocated array in swift?

I want to create a struct in swift that has a small fixed number of values (say 16 floats) as instance data. It is required that this struct not store these values on the heap, so that the address of an instance of the struct is the address of the…
Fooberman
  • 626
  • 5
  • 14
8
votes
1 answer

sizeof compound literal array

I'm trying to statically allocate some structures, each containing two members: a pointer to an array of structures, and the size of that array. Here's a working version of the code: #define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0])) struct…
Chase Patterson
  • 169
  • 1
  • 8
8
votes
4 answers

Returning 'c_str' from a function

This is from a small library that I found online: const char* GetHandStateBrief(const PostFlopState* state) { static std::ostringstream out; // ... rest of the function ... return out.str().c_str() } In my code I am doing this: const…
user199421
  • 1,850
  • 2
  • 18
  • 18
7
votes
5 answers

Returning a pointer to a static buffer

In C on a small embedded system, is there any reason not to do this: const char * filter_something(const char * original, const int max_length) { static char buffer[BUFFER_SIZE]; // checking inputs for safety omitted // copy input to buffer…
danmcb
  • 289
  • 1
  • 12
4
votes
3 answers

Are variables on the stack "statically allocated"?

I was reading this article and saw this: "This article assumes that you already know and understand at least basically how the memory map in GNU/Linux system works, and specially the difference between memory statically allocated in the stack and…
3
votes
8 answers

C++: Why can a statically created variable by passed to a function expecting a reference?

I've been programming in C++ for a while but certainly wouldn't call myself an expert. This question isn't being asked to solve a practical problem that I have, it's more about understanding what C++ is doing. Imagine I have a function that expects…
3
votes
2 answers

How does gcc allocate statically run time known length of array

I wrote the following code: int tester(int n) { int arr[n]; // ... } This code compiled, no warnings, using g++. My question is - how? The parameter n is known just in runtime, in the array is statically allocated. How does gcc compile…
elyashiv
  • 3,623
  • 2
  • 29
  • 52
2
votes
2 answers

C/C++ statically defined circular data forward definition for array

I am looking for the best know method for statically defining C/C++ data structures which need to be circularly linked. E.g. a tree where child and parent both need pointers to each other. extern struct Op op_subops[4]; // fwd ref to children of…
Tim
  • 2,708
  • 1
  • 18
  • 32
2
votes
1 answer

C99: is it possible to design an abstract datatype without dynamic allocation?

I have to design an abstract datatype, but I'm not allowed to use dynamic allocation. Seems a bit tricky... What I currently have: In adt.c: struct adt { bool b; }; const size_t adtSize = sizeof( struct adt ); // Initialisation of the adt void…
Markus
  • 261
  • 1
  • 12
2
votes
2 answers

Are Sub-Arrays Guaranteed to be Allocated Linearly?

I know this answer is in violation of the reinterpret_cast rules but it also presumes that sub-arrays will be allocated linearly. I believed this was not guaranteed, but as I search the standard, I find my confidence wavering. If I statically…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
2 answers

Mmap vs Static allocation for large allocations

I'm allocating a rather large, roughly 100GB, chunk of memory. The exact size is always known at compile time. Should I be allocating statically? static char data[DATA_SIZE]; Or using mmap? data = mmap(NULL, DATA_SIZE, PROT_READ|PROT_WRITE,…
Max
  • 2,760
  • 1
  • 28
  • 47
1
vote
0 answers

Hacker rank questions on finding the odd number

I am trying to do a hacker rank question. The question is simple. But it has asked me to do it using static allocation and dynamic allocation and every other simple question like finding an odd number asks me to use allocation while doing it from C?…
Reactoo
  • 916
  • 2
  • 12
  • 40
1
vote
4 answers

Deallocating locally defined variables in C

Assume we have the following piece of code: void foo() { char buffer[100]; } Is there a (preferably portable) way in C to deallocate buffer from the runtime stack (akin to add esp, 100 in assembly), before foo() returns?
1
2 3