Thread: malloc(); and free();

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    malloc(); and free();

    What do malloc(); and free(); do?

    What are they used for? Could somebody please show me an example?

    Also, what header files do i include to use these functions?

    I use MSVC++ 6!

    Cya.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    malloc is used for dynamic memory allocation, free is used to free the memory allocated by malloc (or calloc, realloc)

    read the manual pages for more information

    example:
    Code:
    #include <stdio.h>
    #include <stdlib.h> /* needed for malloc, free */
    
    int main(void)
    {
       char *msg;
    
       msg = malloc(13); /* allocate memory (13 bytes) */
       if(msg == NULL)
          return -1;
    
       strcpy(msg, "Hello world!");
       printf("%s\n", msg);
       free(msg);
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc calloc and free
    By -EquinoX- in forum C Programming
    Replies: 27
    Last Post: 03-26-2009, 10:59 AM
  2. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  3. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  4. Ask about free funtion using with malloc
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 04:43 PM
  5. Malloc and Free.....
    By heljy in forum C Programming
    Replies: 5
    Last Post: 04-14-2002, 09:17 PM