Reputation:
I have an array of pointers to structs:
item** items = NULL;
These are the functions I wrote for allocating and printing the array:
void allocateItems(item** items, int numItems) {
items =malloc(numItems*sizeof(item*));
for (int i = 0; i < numItems; i++) {
items[i]=malloc(sizeof(item*));
items[i]->data = i + 1;
items[i]->data2 = (i + 1) % 2;
}
}
void printItems(item** items, int numItems) {
for (int i = 0; i < numItems; i++) {
printf("%d : %d\n", items[i]->data, items[i]->data2);
}
}
But when I try to print that array of structs, I get a segmentation fault. Can someone tell me where the mistake is?
Upvotes: 0
Views: 1046
Reputation: 121971
This is only allocating enough memory for an item*
:
items[i]=malloc(sizeof(item*));
it must allocate memoryfor an item
:
items[i]=malloc(sizeof(item));
In order for the changes made with allocateItems()
to be visible to the caller then address of items
must be passed in:
allocateItems(&items, 4);
which the changes the argument type and means items
must be dereferenced within the function:
void allocateItems(item*** items, int numItems)
{
*items = malloc(numItem * sizeof(item*));
for (int i = 0; i < numItems; i++)
{
(*items)[i] = malloc(sizeof(item));
(*items)[i]->data = i + 1;
(*items)[i]->data2 = (i + 1) % 2;
}
}
This could be simplified by allocating an array of item
instead of an array of item*
. It would also mean that the list of items
are in a contiguous block of memory instead of being fragmented with the multiple malloc()
for allocating each element in the array:
void allocateItems(item** items, const int numItems)
{
*items = malloc(numItems * sizeof(item));
if (*items)
{
for (int i = 0; i < numItems; i++)
{
(*items)[i].data = i + 1;
(*items)[i].data2 = (i + 1) % 2;
}
}
}
item* items;
allocateItems(&items, 4);
if (items)
{
for (int i = 0; i < 4; i++)
printf("[%d]: (%d, %d)\n",
i,
items[i].data,
items[i].data2);
free(items);
}
Upvotes: 6