0

I am beginner when it comes to C Programming language.

I want to read a file block of 1 KB in memory and store it in a In-Memory HashTable. To handle collision I have a doubly linked list for each bucket in my HashTable.

Suppose I model a node of my linked list as a structure with next and previous pointer. How should I be storing this file block of 1 KB?

Any pointers or ideas appreciated.

1 Answer 1

1
typedef struct struct_FILEBLOCK FILEBLOCK ; struct struct_FILEBLOCK { char *data ; FILEBLOCK *next ; FILEBLOCK *prev ; } ; 

then all your actual data blocks can be exactly 1k long, and your list elements tiny.

FILEBLOCK *fballoc(void) { FILEBLOCK *fb = malloc(sizeof(FILEBLOCK)) ; fb->data = malloc(1024) ; fb->next = 0 ; fb->prev = 0 ; return fb ; } 

then

void fbread(int fh,FBLOCK *fb) { read(fh,fb->data,1024) ; /* whateva */ } 
Sign up to request clarification or add additional context in comments.

1 Comment

@user1338: the OP probably was thinking of char data[1024] instead of your dynamic allocation. Which approach is preferred ... it depends. Since your FILEBLOCK structure itself is already allocated dynamically, it should be harmless to do so. OTOH if it was an array, I'd rather use dynamic allocation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.