0

Basically I have a function which I would like to push a certain amount of stack space based on an integer variable. So I could make the function take up 3 bytes one time, then 5 or 6 another time. I need this to be on the stack not the heap, does anyone know how to do this via inserting assembly into my code or something?

void Bar::foo(int alloc){
   //allocate data on stack the size of alloc
}
Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
me me
  • 778
  • 2
  • 7
  • 18
  • 1
    Don't you increase the stack just by changing the stack pointer? – Jiminion Jul 26 '13 at 01:18
  • This is clearly a platform-specific thing. On Linux, there is [alloca](http://man7.org/linux/man-pages/man3/alloca.3.html). Consider [Why is alloca not considered good practice](http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice) as well. – jogojapan Jul 26 '13 at 01:18
  • Windows/VC++: http://stackoverflow.com/questions/6704879/header-alloca-h-in-windows – jogojapan Jul 26 '13 at 01:26
  • This is actually a duplicate: [C++ How to allocate memory dynamically on stack?](http://stackoverflow.com/questions/6335023/c-how-to-allocate-memory-dynamically-on-stack) – jogojapan Jul 26 '13 at 01:29
  • You can't do this from within a function, since the stack gets reset when you return. – Mark Ransom Jul 26 '13 at 02:11
  • @meme why must it be on the stack? It might improve answers. – Yakk - Adam Nevraumont Jul 26 '13 at 02:21

3 Answers3

1

I believe alloca is the function you are looking for.

Melflex
  • 26
  • 1
0

spointer = spointer - alloc; // Is it plus or minus...?

Jiminion
  • 5,080
  • 1
  • 31
  • 54
  • Makes sense, good link! I figured it would POP ok, but other entities relying on it makes sense too. I guess there are some benefits vs. using the original position when the function started. – Jiminion Jul 26 '13 at 01:39
0

C99 as well as (according the current drafts) C++14 provide dynamic arrays, i.e. arrays of automatic storage, but run-time determined size.

Since the Standard does not mention the stack (at least not in the sense of a predetermined location in memory), there is no guarantee that dynamic arrays are allocated on the stack, but the general understanding is that they are.

The implementation provided by GCC, for example, guarantees it: http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

Here is how you can use dynamic automatic arrays to allocate a buffer of char according to your specifications:

#include <iostream>
#include <algorithm>

void foo(int alloc) {

  /* Allocate data on the stack, the size of `alloc`. */
  char c[alloc];

  /* Do something with the buffer. I fill it with 'a's. */
  std::fill(c,c+alloc,'a');

  /* And print it. */    
  for (int i = 0; i < alloc; ++i)
    std::cout << c[i];

  std::cout << std::endl;

}

int main()
{
  int alloc;
  std::cin >> alloc;
  foo(alloc);
  return 0;
}

(Clearly, if you want to use this buffer to store objects larger than char, you might need to make sure of alignment, i.e. you might have to move a few bytes inside the buffer to get a properly aligned address.)


C++14 is also likely to provide a C++-style container for dynamic automatic arrays, std::dynarray. The idea is that they should be allocated on the stack, too, if possible (cf. Why both runtime-sized arrays and std::dynarray in C++14?).

Community
  • 1
  • 1
jogojapan
  • 68,383
  • 11
  • 101
  • 131