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>malloc() allocate memory block</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 malloc()                Allocate Memory Block

 #include   <malloc.h>                   Required for declarations only
 #include   <stdlib.h>                   For ANSI compatibility

 void       *malloc(size);
 size_t size;                            Number of bytes to allocate

    malloc() allocates a block of memory of 'size' bytes.

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

      Notes:    Use free() to deallocate block allocated with malloc().

                malloc() may allocate a block larger than 'size' bytes
                because of space required for alignment and DOS
                housekeeping.  The space is always suitably aligned for
                storage of any type of object. (Use a cast on the return
                value if a pointer to a type other than void is needed.)

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

    The following statements allocate space for 1000 bytes and then free
    the allocated space.

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

           char *memptr;

           main()
           {
               if ((memptr = malloc(1000)) == NULL)
                    printf("not enough room to allocate memory\n");
               else {
                    .
                    .
                    .
                    free(memptr);
               }
           }



See Also: calloc() halloc() realloc() free()

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