1

Im toying with creating my own hash table data structure, but I have hit an unexpected problem, which I can't solve and haven't found a satisfying solution for it yet.

See, I have this linked list struct

struct Link
{
   int v;
   struct Link* next;
}

and then inside the hash table structure I want keep track of an array linked lists, like this:

struct Link** entries;

What I run into is, that for this to work, I first have to initialize the array like this:

entries = malloc(sizeof(struct Link*) * N);

for (i = 0; i < N; i++)
   entries[i] = malloc(sizeof(struct Link));

What I want is not having to do the for loop that initializes the structs, because that's not how linked lists work, I want to leave entries[x] blank until it actually gets assigned a value.

If I don't do the for loop this happens:

if (entries[x] != NULL)  /* true, the array is initialized */
  entries[x]->v = value; /* SEGFAULT, there is no struct initialized */

That if statement 'should' return false if I haven't assigned a Link struct to it yet, but it doesn't.

One way of solving this problem is to just initialize all the first links of the linked list with that for loop and then checking for the value, but that's not what I want.

So does anyone know a way to solve this the way I want it to?

3
  • but that's not what I want Why is that ? Commented Sep 1, 2012 at 8:24
  • Because you don't want to have an 'empty' link on the start of the linked list. Commented Sep 1, 2012 at 8:28
  • you can use calloc instead of using malloc when allocating entries. so your checking extires[x] against NULL works better. Commented Sep 1, 2012 at 8:38

3 Answers 3

1

You can use calloc rather than malloc while allocating entries like:

entries = calloc(sizeof(struct Link*),  N);
Sign up to request clarification or add additional context in comments.

2 Comments

@Rohan As a sidenote, you swapped the calloc parameters, although it doesn't really matter. The first parameter should be N and the second is the size.
Strictly speaking calloc is not the right tool to initialize pointer values. There seem to be architectures out there for which a null pointer is not represented by an all 0 byte pattern. So be a bit careful when using this.
1

Well, you can't. Uninitialized pointers are not necessarily NULL.

Comments

0

You will need to initialize the pointer array after you allocate space for it with malloc(). You can use another for-loop like:

for (i = 0; i < N; i++)
{
   entries[i] = NULL;
}

or simply use calloc() to allocate the array. This will set all elements to 0.

entries = calloc(N, sizeof(struct Link*));

Afterwards you need to allocate the elements as needed:

if (entries[x] == NULL)
{
   entries[x] = malloc(sizeof(struct Link));
}
entries[x]->v = value; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.