Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>alloca() allocate memory block on stack</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 alloca()                Allocate Memory Block on Stack

 #include   <malloc.h>                   Required for declarations only

 void *alloca(size);                     Returns pointer to allocated space
 size_t size;                            Number of bytes to allocate on stack

    alloca() allocates 'size' bytes on the program's stack. When the
    function that called alloca() is exited, the allocated space is freed
    automatically.

    Returns:    A char pointer to allocated space.  Returns NULL (defined
                in <stdio.h>) if the space cannot be allocated.

      Notes:    Do not pass the pointer returned by alloca() as an
                argument to free(); the space will be automatically freed
                upon exit from the function that called alloca().

                Because alloca() manipulates the stack, use it only in
                simple assignment statements. NEVER use it in an
                expression that is an argument to a function.

 Portability:   Not supported by ANSI standard.

   -------------------------------- Example ---------------------------------

    The following statements demonstrate a call to alloca() that
    allocates space for 20 integers on the stack:

           #include <malloc.h>
           #include <stdio.h>       /* for printf and NULL */

           func(x)
           int x;
           {
               int *intptr;

               /* allocate 'x' ints in a simple expression */

               intptr = (int *) alloca(x * sizeof(int));
               if (intptr == NULL) {
                  printf("not enough stack space\n");
                  return;
               }
               .
               .
               /* 'intptr' is automatically "freed" on exit */
            }

           main()
           {
               func(20);
           }


See Also: calloc() malloc() realloc() stackavail()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson