Thread: seg fault in vector delete function

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    CA
    Posts
    19

    seg fault in vector delete function

    I am trying to write a vector dispose function that frees the individual elements addressed in my vector. Basically v-> elems points to the first element in an an array of elements, each of which is elemSize long. alloclength is the length that I have allocated. Because this is a generic vector implementation vfree is supplied by the client when s/he wants to free the memory associated with a particular element pointed to from teh array.

    Code deleted
    http://cboard.cprogramming.com/showp...49&postcount=8
    Last edited by Salem; 10-18-2008 at 11:36 PM. Reason: You were warned

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, here's the deal. Before you paste in your code, type "[code]". Then, after the code is all in there, type "[/code]". Learn it, live it.

    If calling vfree gives you a segfault, why wouldn't you expect the problem to be in vfree? (Which is still probably trying to free a pointer that wasn't malloced.)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    CA
    Posts
    19
    here is the code for vfree (it is being called on to pointers to strings (char*'s, dynamically allocated):

    Code:
    
    /** 
     * Function: FreeString
     * --------------------
     * Understands how to free a C-string.  This
     * function should be used by all vectors that
     * store char *'s (but only when those char *s
     * point to dynamically allocated memory, as
     * they do with strings.)
     */
    
    static void FreeString(void *elemAddr)
    {
      char *s = *(char **) elemAddr;
      free(s); 
    }
    
    /** 
     * Function: PrintString
     * ---------------------
     * Understands how to print a C-string stored
     * inside a vector.  The target FILE * should
     * be passed in via the auxData parameter.
     */
    
    static void PrintString(void *elemAddr, void *auxData)
    {
      char *word = *(char **)elemAddr;
      FILE *fp = (FILE *) auxData;
      fprintf(fp, "\t%s\n", word);
    }
    the thing is that this code was given tome by the project file so it cant be wrong..i suspect there is something wrong what i am calling vfree ON...?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    FreeString expects a pointer to the thing being freed, not the thing itself. For this to work, v->elems needs to be a pointer to a pointer, in other words it needs to be set up like so:
    Code:
    v->elems = malloc(10 * sizeof(char *));
    v->elems[0] = malloc(256); // a C-string of 256 chars -- this is what will get freed by FreeString
    v->elems[1] = malloc(256); // and so on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Pointer To Functions = Seg Fault
    By misplaced in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2005, 08:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM