tags:

views:

47

answers:

2

Seem to have a memory allocation problem and think it's because in my struct, there is a pointer to an array of another struct. However, I'm not initializing this array and not sure how:

typedef struct listitem {
    struct listitem *next;
    Entry *entry;
} ListItem;

typedef struct list {
    ListItem *table[100];
} List;

List *initialize(void)
{
    List *tmp;

    if ((tmp = (List *)malloc(sizeof(List))) == NULL)
        return NULL;
    return tmp;
}

Hope that makes sense and you could help!

+3  A: 

You will need to call malloc again.

typedef struct listitem {
    struct listitem *next;
    Entry *entry;
} ListItem;

typedef struct list {
    ListItem *table[100];
} List;

List *initialize(void)
{
    List *tmp;

    if (!(tmp = (List *)malloc(sizeof(List))))
        return NULL;
    for(int i = 0; i < 100; i++) {
        tmp->table[i] = (ListItem*)malloc(sizeof(ListItem));
    }
    return tmp;
}
DeadMG
How can you just access table though, you're not setting the pointer in the struct to it?
Igor K
@Igor K: Whoops, my mistake. Used to proper member functions.
DeadMG
Thanks @DeadMG, tried this however I still get the same error with my program, although thats another question!
Igor K
A: 
bzero(tmp, sizeof(*tmp));

just zeroes the contents of your struct list. If that is what you want.

svenor