Hello,

I'm trying to read dictionary words from a file and store the output in a struct element of type unsigned char *. I keep getting a bunch of memory related errors and segmentation faults which Im sure are totally related to me trying to access something that's not really there. I'm just not sure how to get around my errors. Here's my truncated code (the parts that concern my problem so far)

What I would like to do eventually is to calculate the hash value of a given word, which is argv[2] in this example, and calculate the hash value of each word in the given filename (argv[1]), and compare whether they match or no. If match, display "found", otherwise, "not found".

#include <stdlib.h>
#include <stdio.h>
#include <cuda.h>
#include <cutil.h>
#include "common.h"

#define MAX_THREADS_PER_BLOCK 128

typedef struct {
	unsigned const char *data;
	unsigned const char *hash;
} testvector;

int main(int argc, char *argv[])
{
	if(argc!=3)
		printf("usage: ./sha1test <filename> <searchword>\n");

	else{
		FILE* fp_in = fopen(argv[1], "r");
		int count = 0;
		testvector *tv;
		tv = (testvector *) malloc(sizeof(testvector));
		if(!fp_in)
			printf("can't open file\n");

		unsigned char gHash[20];
		sha1_cpu ((unsigned char*)argv[2], strlen((const char*)argv[2]), gHash);
		printf("Your word is:\t%s\n",argv[2]); //display <searchword>
		printf("The hash is:\t"); //display its calculated hash

		int ii;
		for(ii=0; ii<20; ii++)
			printf("%x", gHash[ii]);
		printf("\n");

//read words from file and calculate their hash values
		while(!feof(fp_in)){
			size_t bytes = 128;
			unsigned char d[bytes];
			fgets((char *)(&d[0]),bytes,fp_in);
//			d = (unsigned char *) malloc(50*sizeof(unsigned char));
			count++;
			free(d);
		}
		fclose(fp_in);
	}
	return 0;
}

Would appreciate any help.

Too many loop holes as I see it :

1>You need to work on your hash function and also your hashing concept. If you are calculating the hash value of the given word and calculating the hash value of all the words in a file and then matching it,don't you think you are making hash work just as efficiently as linear search ??? Hash should have a complexity of O(1) and you have reduced it to O(n).So work on it.

2>I don't see you freeing the space allocated for the test vector,nor can I see memory being allocated for the data members of the structure testvector.

3>Within the while loop why are you using the function free(d) ??? You are directly allocating memory for d as :

unsigned char d[bytes];

You are not allocating it memory from the heap(dynamic memory) so you cannot use free here like this.

4>And I hope you know that you are trying to work in C and not in C++ and even this forum is for C problems.This code of yours is just out of boundaries,initializing variables wherever whenever required.C will never work like this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.