Some help with "realloc invalid next size" problem?

This is a discussion on Some help with "realloc invalid next size" problem? within the C Programming forums, part of the General Programming Boards category; Hello everyone. I'm new in this forum, but I have quite some time reading in the background all the amazing ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    8

    Unhappy Some help with "realloc invalid next size" problem?

    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, the problem is probably the use of pointers to pointers to chars, and the realloc function... Because I increment the size of the DICC array of pointers, and then malloc a size to put a word in the DICC[cant] space that I've created with realloc... I know it's a bit confusing at first, but I don't know what the problem is exactly... I'm working with linux, by the way...

    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(archivo);
           		free(word);
    		free(TEMP);
    	}
    }
    Every help would be apreciate...
    I'm trying to eliminate memory leaks issues and nasty things like that...



    Edit: By the way... If I comment the lines
    Code:
    		  		DICC[cant] = malloc(strlen(word)+1);
    		  		strncpy(DICC[cant],word);
    The dictionary loads completly and at the end it shows a segmentation problem (I supose because 'm not free'ing the memory of DICC yet...)
    Last edited by ninboy; 05-20-2008 at 09:34 AM. Reason: More info to get help :S

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To begin with:
    Code:
    char *word = malloc(sizeof(char));
    One char is just enough to store the null-terminator. You can't use word for anything else.

    I also think that realloc is meant for asking for more memory to a pointer obtained through a previous call to malloc (not a NULL pointer).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    8
    Quote Originally Posted by anon View Post
    To begin with:
    Code:
    char *word = malloc(sizeof(char));
    One char is just enough to store the null-terminator. You can't use word for anything else.

    I also think that realloc is meant for asking for more memory to a pointer obtained through a previous call to malloc (not a NULL pointer).
    Good point about the size of word.

    But about realloc, what I read in the manuals is that malloc(size) is the same as realloc(null, size)...
    But let me try using malloc first...

    Edit: The segmentation problem is fixed now, i've changed the size of the malloc of the word...
    But... The realloc problem is still here... In my pc the script does 3 iterations "perfectly" and then dies with the glibc error message...
    Last edited by ninboy; 05-20-2008 at 10:09 AM.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    8
    FIXED!!!

    anon, you gave me the hint, thanks! I wasn't asigning te proper amount of memory to the array of pointers to pointers, so i was getting into parts of memory that i shouldn't have been touching!

    So, if anyone wants to know, What i did was:

    Change
    Code:
    char *word = malloc(sizeof(char));
    for
    Code:
    char *word = malloc(sizeof(char)*100);
    And change
    Code:
    TEMP = realloc( DICC, cant+sizeof(char*));
    for
    Code:
    TEMP = realloc( DICC, (cant+1)*sizeof(char*));

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    By the way, you can change:
    Code:
    char *word = malloc(sizeof(char)*100);
    to:
    Code:
    char *word = malloc(100);
    since sizeof(char) is always 1. sizeof(char*), on the other hand, depends on the implementation.

    Also, is there any reason to use the names TEMP and DICC instead of temp and dicc? Fully capitalised names tend to be reserved for macro names (or constants), by common convention.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    8
    Thanks for the sugestions, I'll change that. I though that malloc always need to have a sizeof(---), thanks for the new definition, hehehe.

    and about DICC, well, yeah. It like a global variable, a "constant" variable, that only can change in the moment you load the dictionary in the memory. I use capital letters because it's like a global variable... those variables have a different covention to write their names?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    It is likely that some of your issues stems from the fact that you're doing buffer overruns:
    http://cpwiki.sourceforge.net/Buffer_overrun
    There are some tools to detect that and there are also some safe libraries that detects buffer overruns. Read the article for more information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. selection problem
    By Ken JS in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 09:47 PM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21