Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <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

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

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

    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, it should be used
                only in simple assignment statements and NEVER in an
                expression that is an argument to a function.

  -------------------------------- 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