I have some C code that contains an array of text that I'm trying to manipulate in the following manner :-
- Allocate an array of pointers
dictionaryof sizedictionary_sizeinitialized to 50 - Replace all spaces and \n's with '\0'
- Store the address of every 3rd string(separated by an unknown number of \n's or spaces) in
dictionary - If
dictionaryis full, realloc to size,dictionary_size * 2
The code however, causes the following error :-
*** glibc detected *** ./crack: realloc(): invalid next size: 0x0000000001386010 ***
^Cmake: *** [run] Interrupt
The code is as follows :-
// Replace all spaces with '\0'
for ( i = 0; i < file_size; i++ ) {
if ( temp_buffer[i] == ' ' || temp_buffer[i] == '\n' ) {
while ( temp_buffer[i] == ' ' || temp_buffer[i] == '\n' ) {
temp_buffer[i] = '\0';
}
j++;
}
if ( (j-1) % 3 == 0 ) {
dictionary[k] = temp_buffer+i;
k += 1;
if ( k == dictionary_size ) {
dictionary_size *= 2;
printf("Going to realloc to %d\n", dictionary_size);
dictionary = (char **)realloc(dictionary, dictionary_size);
}
}
}
[EDIT] Based on the debugging statements I have, the very first realloc(to a size of 100) fails.
dictionary == NULLor ever set based on a return frommalloc(), or isrealloc()the first thing you ever do? Second, invalid next size: 0x0000000001386010 suggests the size is not 100... have you confirmed at runtime that it's what you think it should be?realloc()but to an internally held value which is interpreted as an internal size of some management object and the size had been detected as being invalid. this is fatal. Check the program's memory allocation/freeing/access using a memchecker tool like Valgrind: valgrind.org