R
RubenDV
I am writing code for a dictionary type in c, but i have some problems
allocating the memory for it. The dictionary type itself is a struct
with the following contents:
typedef struct {
char ** keys;
char ** content;
int count; /* Amount of space actually used */
int allocated_space; /* Amount of space allocated for the keys and
content */
} dict_t;
The function that allocates the memory goes like this:
dict_t * dictWithAllocatedSpace(dict_t * dictionary, int size)
{
dictionary = (dict_t *)malloc(sizeof(dict_t));
dictionary->allocated_space = size;
dictionary->keys = (char **)malloc(size);
dictionary->content = (char **)malloc(size);
dictionary->count = 0;
return dictionary;
}
when this function is executed the pointer it returns is not NULL, but
when i try to access one of its members, i get a bus error. I probably
made a terrible mistake, but i have no idea what that could be.
Any help is appreciated!
Ruben
allocating the memory for it. The dictionary type itself is a struct
with the following contents:
typedef struct {
char ** keys;
char ** content;
int count; /* Amount of space actually used */
int allocated_space; /* Amount of space allocated for the keys and
content */
} dict_t;
The function that allocates the memory goes like this:
dict_t * dictWithAllocatedSpace(dict_t * dictionary, int size)
{
dictionary = (dict_t *)malloc(sizeof(dict_t));
dictionary->allocated_space = size;
dictionary->keys = (char **)malloc(size);
dictionary->content = (char **)malloc(size);
dictionary->count = 0;
return dictionary;
}
when this function is executed the pointer it returns is not NULL, but
when i try to access one of its members, i get a bus error. I probably
made a terrible mistake, but i have no idea what that could be.
Any help is appreciated!
Ruben