malloc & free

This is a discussion on malloc & free within the C Programming forums, part of the General Programming Boards category; hi; do i need to use free with each malloc? how could i free memory in this function: Code: char ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    malloc & free

    hi;
    do i need to use free with each malloc?
    how could i free memory in this function:

    Code:
    char *function(){
    char * p=malloc(10*sizeof(char));
    .......
    ....
    return p;
    
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    As a general rule you should.

    It's also a 'guideline' to put the corresponding free() with it's malloc, but not possible in all cases, such as this.

    Simply call free() on the pointer returned by 'function'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  3. String malloc & free?
    By The Tracker in forum C Programming
    Replies: 2
    Last Post: 05-22-2002, 10:45 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21