简体   繁体   中英

Malloc large space of struct and accessing it like arrays in c

I'm dealing with implementing a hash table. My understanding of a hashtable is that is that to have an array like table where you're able to access the elements quickly by getting the hash value and modding it by the table size. So my initial thought was declaring

Node *hTable [100];

where

typedef struct node {
    char *s; 
    int value;
} Node;

and going to the index of the array and malloc a new element that belongs there. But, the problem is that I need to grow my table.

So, my question is, how would I make a dynamic table, but access it like an array? (eg table[i] ).

I know that you need to call something like

Node *table = (Node*)malloc(sizeof(Node)*size);

which lets you access it like a table table[i] =... but if I did that, I can't declare a new Node in the index of the table

table[i]=(Node*)malloc(sizeof(Node));

Here's a code that I've been testing with (getting seg fault) to better give a view of the problem:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3
  4 typedef struct node {
  5         int data;
  6         struct node *next;
  7 } Node;
  8
  9
 10 void main() {
 11         Node **list;
 12         *list = (Node*)malloc(sizeof(Node)*10);
 13         for (int i = 0; i < 10; i++) {
 14                 list[i] = (Node*)malloc(sizeof(Node)); //problem here?
 15                 list[i]->data = i;
 16                 list[i]->next = NULL;
 17         }
 18         printf("printing...\n");
 19         for (int i = 0; i < 10; i++) {
 20                 printf("%d ", list[i]->data);
 21         }
 22 }

Your problem is how you allocate space for list . list is uninitialized and does not point to valid memory, you must allocate space for it first, and then allocate space for each element:

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data;
    struct node *next;
} Node;


int main() //return type of main is int
{
    Node **list;
    list = malloc(10 * sizeof *list); //allocate memory for list not *list, also no need to cast return value of malloc.
    for (int i = 0; i < 10; i++)
    {
        list[i] = malloc(sizeof *list[i]); //allocate space for each element.
        list[i]->data = i;
        list[i]->next = NULL;
    }

    printf("printing...\n");

    for (int i = 0; i < 10; i++)
    {
        printf("%d ", list[i]->data);
    }

    return 0;
}
 #include <stdio.h>
 #include <stdlib.h>

 typedef struct node {
         int data;
         struct node *next;
 } Node;


 int main() {
         // just initialize it this way ( previous was undefined behavior, dereferencing an initialize pointer)
         Node **list= malloc(sizeof(Node*)*10);
         for (int i = 0; i < 10; i++) {
                 list[i] = malloc(sizeof(Node*)); //problem here?
                 list[i]->data = i;
                 list[i]->next = NULL;
         }
         printf("printing...\n");
         for (int i = 0; i < 10; i++) {
                 printf("%d ", list[i]->data);
          }
 }

It doesn't need to be an array of pointers, you can simply make an array of nodes, with:

Node *list = malloc(sizeof *list * count);

Then you can access list[i].s and list[i].value .

When you want to grow the table, you use realloc() :

new_list = realloc(list, sizeof *list * new_count);
if (new_list) {
    list = new_list;
} else {
    // report allocation failure
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM