-2

I'm working on some homework for my C class and I'm not sure how to approach this. I'm told to 'allocate from the heap a hash table with htsize buckets, each initially empty.' I've already made htsize specifiable via command line arguments and made it a global variable in the following code:

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

int htsize;
int main(int argc, char *argv[])
{
    if (argc <= 1)
    {
        printf("Please declare a table size");
        return 1;
    }
    htsize = atoi(argv[1]);
}

but I am not sure what 'allocating from the heap' entails. Can someone help break this down for me?

dave
  • 4,812
  • 4
  • 25
  • 38
donut juice
  • 257
  • 6
  • 19
  • 2
    C has [no standard hash table](http://stackoverflow.com/questions/6118539/hashtable-as-part-of-standard-c-library). You should have either gotten a library to work with, or the assignment is to write one yourself. Either way, the question is completely unclear as it stands. – MicroVirus Nov 05 '15 at 23:50
  • @MicroVirus I do have to create one myself, I just need clarification on what the directions mean by 'allocating from the heap' – donut juice Nov 05 '15 at 23:53
  • allocating from the heap means dynamic allocation. You will have to use malloc to achieve this. Do you know the concept of a hash table? – victor Nov 05 '15 at 23:54
  • One thing I can elaborate on: from the heap means allocated using `malloc` or the likes. – MicroVirus Nov 05 '15 at 23:54
  • See [What is a Memory Heap?](http://stackoverflow.com/questions/2308751/what-is-a-memory-heap) then. – MicroVirus Nov 05 '15 at 23:55
  • Why downvote the question? Asking about dynamic allocation is a legitimate beginner's question. – victor Nov 05 '15 at 23:56
  • @victor Probably because a simple search could've answered that part. – MicroVirus Nov 05 '15 at 23:57
  • @victor yes I am familiar with the concept of hash tables, I just wasn't sure of how I should be allocating things. It's all clear now, thanks. – donut juice Nov 05 '15 at 23:59
  • @MicroVirus "allocating from the heap [c]" doesn't turn up any clear results on SO. That is probably an oversight. Then again this question is titled "How do I initialize a hash table" when what they want to ask is "what is allocating from the heap?" – dave Nov 06 '15 at 00:01
  • @dave My Google query in broken English of "allocate on heap mean" found the question I linked. Google-fu and search skills in general are good tools to have in ones skillset. – MicroVirus Nov 06 '15 at 00:03
  • Yeah, that's what I should have tried, searching SO without resorting to Google always gives horrible results. – dave Nov 06 '15 at 00:06

1 Answers1

0

C does not provides standard data type hash table, you would have to implement one. Plus avoid using atoi because it is a very 'unsafe' function and it's use is typically avoided.

Already made implementations are widely available for example: https://gist.github.com/tonious/1377667

PeCosta
  • 537
  • 4
  • 13