Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. freeing pointers in pointer array causes SegFault [solved]

freeing pointers in pointer array causes SegFault [solved]

Scheduled Pinned Locked Moved ATL / WTL / STL
data-structures
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jordanwb
    wrote on last edited by
    #1

    Hello. I have the following code block:

    #include <stdlib.h>

    typedef struct List_s
    {
    int count;
    void **items;
    } List;

    List *list_create()
    {
    List *list = malloc (sizeof (List));
    list->count = 0;
    list->items = malloc (1 * sizeof (void *));

    return list;
    

    }

    void list_add (List *list, void *item)
    {
    list->items = realloc (list->items, (list->count + 1) * sizeof (void *));
    list->items[list->count] = item;
    list->count++;
    }

    void list_dealloc (List *list)
    {
    for (int i = 0; i < list->count; i++)
    {
    free (list->items[i]);
    }

    free (list->items);
    free (list);
    

    }

    When I pass a List to list_dealloc I get a segmentation fault on the line in the for loop. I can print the contents of items[i] just fine (in my test case it's the string "Hello"). I'm using gcc-4.5.1 on 64 bit Fedora 14 if that's anything useful.

    modified on Monday, February 28, 2011 12:57 PM

    C L 2 Replies Last reply
    0
    • J Jordanwb

      Hello. I have the following code block:

      #include <stdlib.h>

      typedef struct List_s
      {
      int count;
      void **items;
      } List;

      List *list_create()
      {
      List *list = malloc (sizeof (List));
      list->count = 0;
      list->items = malloc (1 * sizeof (void *));

      return list;
      

      }

      void list_add (List *list, void *item)
      {
      list->items = realloc (list->items, (list->count + 1) * sizeof (void *));
      list->items[list->count] = item;
      list->count++;
      }

      void list_dealloc (List *list)
      {
      for (int i = 0; i < list->count; i++)
      {
      free (list->items[i]);
      }

      free (list->items);
      free (list);
      

      }

      When I pass a List to list_dealloc I get a segmentation fault on the line in the for loop. I can print the contents of items[i] just fine (in my test case it's the string "Hello"). I'm using gcc-4.5.1 on 64 bit Fedora 14 if that's anything useful.

      modified on Monday, February 28, 2011 12:57 PM

      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #2

      Jordanwb wrote:

      segmentation fault on the line in the for loop

      If the crash is at free() function, ensure that you are passing a heap pointer as item to list_add() function, not the address of a stack variable.

      1 Reply Last reply
      0
      • J Jordanwb

        Hello. I have the following code block:

        #include <stdlib.h>

        typedef struct List_s
        {
        int count;
        void **items;
        } List;

        List *list_create()
        {
        List *list = malloc (sizeof (List));
        list->count = 0;
        list->items = malloc (1 * sizeof (void *));

        return list;
        

        }

        void list_add (List *list, void *item)
        {
        list->items = realloc (list->items, (list->count + 1) * sizeof (void *));
        list->items[list->count] = item;
        list->count++;
        }

        void list_dealloc (List *list)
        {
        for (int i = 0; i < list->count; i++)
        {
        free (list->items[i]);
        }

        free (list->items);
        free (list);
        

        }

        When I pass a List to list_dealloc I get a segmentation fault on the line in the for loop. I can print the contents of items[i] just fine (in my test case it's the string "Hello"). I'm using gcc-4.5.1 on 64 bit Fedora 14 if that's anything useful.

        modified on Monday, February 28, 2011 12:57 PM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Are you sure that all the items passed in to your list through the list_add() function have been created by malloc()?

        I must get a clever new signature for 2011.

        J 1 Reply Last reply
        0
        • L Lost User

          Are you sure that all the items passed in to your list through the list_add() function have been created by malloc()?

          I must get a clever new signature for 2011.

          J Offline
          J Offline
          Jordanwb
          wrote on last edited by
          #4

          Thanks for replying. This is what's in my main() function:

          List *list = list_create();
          list_add (list, "Hello");
          list_dealloc (list);

          L 1 Reply Last reply
          0
          • J Jordanwb

            Thanks for replying. This is what's in my main() function:

            List *list = list_create();
            list_add (list, "Hello");
            list_dealloc (list);

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            So you are adding a pointer to a constant string which you later try to free(): result SEGV fault. You need to ensure that every pointer you pass into list_add() is pointing to a memory block that has been returned from malloc(). something like:

            List *list = list_create();
            char* pitem = (char*)malloc(10);
            strcpy(pitem, "Hello");
            list_add (list, pitem);
            // pitem may now be removed with a call to free()
            list_dealloc (list);

            I must get a clever new signature for 2011.

            J 1 Reply Last reply
            0
            • L Lost User

              So you are adding a pointer to a constant string which you later try to free(): result SEGV fault. You need to ensure that every pointer you pass into list_add() is pointing to a memory block that has been returned from malloc(). something like:

              List *list = list_create();
              char* pitem = (char*)malloc(10);
              strcpy(pitem, "Hello");
              list_add (list, pitem);
              // pitem may now be removed with a call to free()
              list_dealloc (list);

              I must get a clever new signature for 2011.

              J Offline
              J Offline
              Jordanwb
              wrote on last edited by
              #6

              Okay thanks.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups