Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

7. Memory Allocation > Allocating Memory on the Stack: alloca()

Allocating Memory on the Stack: alloca()

Like the functions in the malloc package, alloca() allocates memory dynamically. However, instead of obtaining memory from the heap, alloca() obtains memory from the stack by increasing the size of the stack frame. This is possible because the calling function is the one whose stack frame is, by definition, on the top of the stack. Therefore, there is space above the frame for expansion, which can be accomplished by simply modifying the value of the stack pointer.

#include <alloca.h>

void *alloca(size_t size);

Note

Returns pointer to allocated block of memory

The size argument specifies the number of bytes to allocate on the stack. The alloca() function returns a pointer to the allocated memory as its function result.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint