seg fault in vector delete function

This is a discussion on seg fault in vector delete function within the C Programming forums, part of the General Programming Boards category; I am trying to write a vector dispose function that frees the individual elements addressed in my vector. Basically v-> ...

  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
    Issues with pointer and freeing its associated memory
    Last edited by Salem; 10-19-2008 at 12:36 AM. Reason: You were warned

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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,185
    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, 09:27 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Pointer To Functions = Seg Fault
    By misplaced in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2005, 09:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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