Instantiating a hash map in C

Viewed 22

I am trying to instantiate a hash map in c,

Here's the code:

//hash Struct
typedef struct hashMap{
  int** Map;
  int val;
} hashMap;
typedef hashMap* mapPtr;


// Function to create a hashMap object pointer
void createHash(mapPtr aHash){
  // Create a hash object
  if(!aHash){
    aHash = calloc(1, sizeof(hashMap));
    int size = (int) pow(10,9);
    // Allocate Memory for 2d array
    aHash->Map = calloc(2, sizeof(int*));
    // Allocate internal memory for the array -> 10^9 blocks
    for(int i = 2; i < 2; i++){
      aHash->Map[i] = calloc(size, sizeof(int));
    }
    for(int i = 0; i < size; i++){
      aHash->Map[0][i] = -1;
      aHash->Map[1][i] = -1;
    }
    // memset(aHash->Map[0], -1, size*sizeof(int));
  }
}

but I keep getting a segmentation fault, indicating that I'm trying to access memory outside the buffer.

I know that either the memset of or the for loop method would work, but neither seem to be working.

0 Answers
Related