Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 92597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:12:43+00:00 2026-05-10T23:12:43+00:00

I have the following construction: typedef struct bucket { char *key; ENTRY *data; struct

  • 0

I have the following construction:

typedef struct bucket {     char *key;     ENTRY *data;     struct bucket *next; } bucket;  typedef struct {     size_t size;     bucket **table; } hash_table; 

But I have no idea how to allocate memory for that. I tried:

hash_table* ht = malloc(sizeof(hash_table)*101); 

in order to create a hashtable for 101 entries but it din’t work! Can anyone help me? I would really appreciate it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:12:43+00:00Added an answer on May 10, 2026 at 11:12 pm

    Not quite. Assuming this is C, you probably want to make a function:

     hash_table* init_table(size_t size) {      size_t i;      hash_table* ht = (hash_table*)malloc(sizeof(hash_table));      if (ht == NULL) return NULL;      ht->size = size;      ht->table = (bucket**)malloc(sizeof(bucket*)*size);      if (ht->table == NULL) {          free(ht);          return NULL;      }      for (i = 0; i < size; ++i) {          ht->table[i] = NULL;      }      return ht;  } 

    You might need some other fields in that struct.

    If you wanted to be tricky, and never realloc the bucket, you can do this:

     hash_table* init_table(size_t size) {      hash_table* ht = (hash_table*)malloc(sizeof(hash_table)+sizeof(bucket)*size);      if (ht == NULL) return NULL;      ht->size = size;      ht->table = (bucket**)(ht+1);      for (i = 0; i < size; ++i) {          ht->table[i] = NULL;      }      return ht;  } 

    EDIT: I fixed my bucket* table’s to bucket**

    EDIT2: I’ve gotten rid of the memsets and added error checking for malloc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 137k
  • Answers 137k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I almost always use absolute urls, without the site name.… May 12, 2026 at 7:24 am
  • Editorial Team
    Editorial Team added an answer clojure.contrib.server-socket is your friend. Use create-server like so to create… May 12, 2026 at 7:24 am
  • Editorial Team
    Editorial Team added an answer This should work for you. #!/usr/bin/perl use strict; use warnings;… May 12, 2026 at 7:24 am

Related Questions

Within the same compilation unit, the C++ standard says that static initialization order is
I have a smart pointer type, and would like to construct an object that
I'm new to C++, this is my first week since the upgrade from fortran.
Following up from my previous question. Can anyone explain why the following code compiles

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.