Noober
Noober

Reputation: 1626

Memory allocation error in C during hashing

I am new to C programming and I was trying to implement typedef and hashing in my code.But I am getting compilation error when I try to allocate memory -

This is my header file

#define MAX1 11
#define MAX2 23

typedef short IP[4];
typedef enum{TRUE = 1,FALSE = 0}boolean;

typedef struct
{
    IP p;
    char *comp_name;
}Element;

typedef struct
{
    Element e;
    boolean deleted; // deleted flag
    boolean empty;
}Cell;
typedef Cell secLevelHashTable[MAX2];

typedef struct secLevelHashTable *FirstLevelHashTable[MAX1];

typedef struct FirstLevelHashTable hashTable;

This my main code-

#include"hashDef.h"
#include<stdio.h>  
#include<stdlib.h>
void initFirstHTable(hashTable H)
 {
int i,j;
    for(i=0;i<MAX1;i++)
    {
        H. FirstLevelHashTable[i]=(secLevelHashTable *)malloc(sizeof(secLevelHashTable));
        H.FirstLevelHashTable[i]->secLevelHashTable=malloc(sizeof(Cell)*MAX2);
        for(j=0;j<MAX2;j++)
        {
            initSecHTables(H.FirstLevelHashTable[i]->secLevelHashTable[j]);
        }
    }


 }  

 void initSecHTables(Cell *ptr)
 {
    ptr->deleted=0;
    ptr->empty=1;
 }





 int main()
 {
    hashTable h;
    h=malloc(sizeof(FirstLevelHashTable));
    initFirstHTable(h);
    return 0;
 }

This is the error I am getting-

In function ‘main’:
hashOps.c:79:13: error: storage size of ‘h’ isn’t known
   hashTable h;

Upvotes: 1

Views: 104

Answers (1)

morfizm
morfizm

Reputation: 653

Fixed code below. It had numerous small issues and a big one.

Please read the related article the big one:

struct in C: Error storage size of 'params' isn't known -- this will explain "storage size unknown" error; by saying typedef struct FirstLevelHashTable hashTable; you were defining an unfinished struct, rather than referring to existing type.

Header file:

#define MAX1 11
#define MAX2 23

typedef short IP[4];
typedef enum{TRUE = 1,FALSE = 0}boolean;

typedef struct
{
    IP p;
    char *comp_name;
}Element;

typedef struct
{
    Element e;
    boolean deleted; // deleted flag
    boolean empty;
}Cell;

typedef Cell secLevelHashTable[MAX2];
typedef secLevelHashTable* FirstLevelHashTable[MAX1];
typedef FirstLevelHashTable hashTable;

Main code:

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

void initSecHTables(Cell *ptr)
{
   ptr->deleted=0;
   ptr->empty=1;
}

void initFirstHTable(hashTable H)
{
   int i,j;
   for(i=0;i<MAX1;i++)
   {
       H[i]=(secLevelHashTable *)malloc(sizeof(secLevelHashTable));
       for(j=0;j<MAX2;j++)
       {
           initSecHTables(&((*H[i])[j]));
       }
   }
}

int main()
{
   hashTable h;
   initFirstHTable(h);
   return 0;
}

Upvotes: 1

Related Questions