1

The malloc function always allocate memory on the heap. However, while studying the Escape Analylis Article on Wikipedia, I came to know that as an optimization, a compiler can convert heap allocations to stack allocations. For example, if it sees that an allocated memory is only used and then freed inside a function.

Now my question is, is there a way that the programmer can do so himself. That is allocate memory on stack? I know C99 allows a variable to be given as size for an array declaration, but say the programmer wants to resize it. Can it be done?

MetallicPriest
  • 29,191
  • 52
  • 200
  • 356

3 Answers3

5

alloca() is what you're looking for. Of course, if you know your structures dimensions statically, it'd be better to use local variables instead.

SK-logic
  • 9,605
  • 1
  • 23
  • 35
  • You type quicker than me... Anyway, it's worth pointing out [Why is alloca not considered good practice?](http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice) (Your answer is still correct, of course). – Eran Aug 16 '11 at 08:42
  • @eran, well, in context of the escape analysis, I suspect that OP want to use that in some sort of a compiler back-end. In this case it should be safe enough to use. – SK-logic Aug 16 '11 at 08:44
  • 1
    _Shiver_ : `alloca` is not standard C and offers no advantages over VLAs - it's use should be discouraged as much as possible. – paxdiablo Aug 16 '11 at 08:50
  • @paxdiablo, C99 is, unfortunately, also not as standard as it should be - there are many platforms where `alloca()` is available, but no C99. So, if it is for a compiler backend, it would be reasonable to be able to use both. – SK-logic Aug 16 '11 at 08:52
  • C99 _is_ as standard as it should be, it's _the_ standard. I think you may have meant "not as widely supported" :-) – paxdiablo Aug 16 '11 at 08:55
  • @paxdiablo, yes, indeed, I meant it is not so much a de facto standard . – SK-logic Aug 16 '11 at 09:02
  • I will also prefer to not use anything from C99 that is not supported elsewhere. This VLA thing is not even supported in C++. – MetallicPriest Aug 16 '11 at 09:12
4

C99 also allows you to allocate variable length arrays (VLAs) and other variables at any point within a function, not just at the start, so your point about being able to allocate the memory elsewhere is moot.

You can just plug in a:

int arr[somevar];

anywhere in the function and have it actioned.

With VLAs, you no longer need to worry about using malloc/free (or the infamous alloca available on some implementations, which was another way of doing stack-based allocations, the a standing for automatic) for arbitrarily-located variable-sized allocations.

If you want the ability to resize, you're still going to have to use the standard memory allocation functions like malloc and realloc. VLAs don't provide that level of functionality (yet).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

As far as I am aware, converting heap allocations to stack allocations is a library thing, not something that the compiler has any control over. Indeed, on Linux, malloc uses the brk system call (which resizes the stack) rather than use mmap, except for larger pages.

To quote from the source:

The old goals we kept are
  1) try to get the long lived large allocations to use mmap()
  2) really large allocations should always use mmap()
  and we're adding now:
  3) transient allocations should use brk() to avoid forcing the kernel
     having to zero memory over and over again
susmits
  • 2,210
  • 2
  • 23
  • 27
  • Compilers can do it in some cases - see the region analysis implementation in MLkit for example. With C and alike it would be a little bit more problematic due to a non-trivial aliasing analysis. – SK-logic Aug 16 '11 at 08:48
  • `brk` will resize the data segment, not the stack. – Bo Persson Aug 16 '11 at 08:55