malloc and realloc problem

Joined
May 19, 2008
Messages
2
Reaction score
0
Hello everyone. I'm new in this forum, but I have quite some time reading in the background all the amazing help you give here.

I'm trying to write a spellchecker, and I have a dictionary file wich I want to allocate in memory so I can compare strings. The problem is, after a few iterations, glibc show the error:
*** glibc detected *** realloc(): invalid next size: 0x0804a180 ***

Here's the code I'm working on, can someone check it out and tell me what's wrong?

Glosary: DICC is an array of pointers to pointers of chars. Cant is the total amount of words that the program reads from the file, "archivo" is the dictionary file, and that's all...

Code:
void cargaDicc(){
	FILE *archivo;
	char *word = malloc(sizeof(char));
	char **TEMP = NULL;
	char *nombreArch;
	int cant = 0;
	char **DICC = NULL;
	
	archivo=fopen(nombreArch,"r");

 	 if(archivo==NULL) {
    		printf("No se pudo abrir el archivo de diccionario.\n");
  	 }else {
	  	while(fscanf(archivo,"%s",word) != EOF){
	  		
	  		TEMP = realloc( DICC, cant+sizeof(char*));
	  		
			if(TEMP != NULL){
				DICC = TEMP;
		  		DICC[cant] = malloc(strlen(word)+1);
		  		strcpy(DICC[cant],word);
		  		
		  		
		  		cant++;
		  	}else{
		  		printf("No se pudo realocar memoria\n");
				free(DICC);
		  		exit(1);
		  	}
		}
    		printf("Se han cargado satisfactoriamente %d palabras del diccionario.\n", cant);
    		fclose(archivo);
       		free(word);
	}
}

Every help would be apreciate...
I'm trying to eliminate memory leaks issues and nasty things like that...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top