Free allocated space

This is a discussion on Free allocated space within the C Programming forums, part of the General Programming Boards category; I'm calling a function that's allocating space and filling a char array. Code: char **array; size = split(&array, string, delim); ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > (*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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lots of freeware
    By major_small in forum General Discussions
    Replies: 57
    Last Post: 05-28-2014, 04:42 PM
  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

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