Thread: Free allocated space

  1. #1
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79

    Free allocated space

    I'm calling a function that's allocating space and filling a char array.

    Code:
    char **array;
    size = split(&array, string, delim);
    
    free(array)
    
    int split(char ***trgtArrray, char *string, char delim) {
      [...]
      (*trgtArray) = (char **) malloc(count);
      [...]
    
      return count;
    }
    When I try to free **array it crashes. What am I doing wrong?
    Last edited by Fader_Berg; 10-31-2010 at 07:03 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > (*trgtArray) = (char **) malloc(count);
    Maybe, did you really do this?
    (*trgtArray) = (char **) malloc(count * sizeof(char*) );

    Also, see the FAQ on casting malloc in C programs.
    Are you secretly compiling this as a C++ program?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lots of freeware
    By major_small in forum General Discussions
    Replies: 60
    Last Post: 02-14-2017, 03:00 AM
  2. Question about Free() and Ptr-Ptr-Int
    By Kehyn in forum C Programming
    Replies: 11
    Last Post: 04-14-2010, 08:37 PM
  3. How to properly free a pointer and its allocated memory
    By yuzhangoscar in forum C Programming
    Replies: 2
    Last Post: 09-16-2008, 06:26 AM
  4. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  5. Free store vs. heap
    By CondorMan in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2006, 11:41 AM