freeing pointers in pointer array causes SegFault [solved]
-
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
-
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
-
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
-
Are you sure that all the items passed in to your list through the
list_add()
function have been created bymalloc()
?I must get a clever new signature for 2011.
-
Thanks for replying. This is what's in my main() function:
List *list = list_create();
list_add (list, "Hello");
list_dealloc (list);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 intolist_add()
is pointing to a memory block that has been returned frommalloc()
. 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.
-
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 intolist_add()
is pointing to a memory block that has been returned frommalloc()
. 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.